validate.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * 验证类
  4. *
  5. * @package
  6. */
  7. defined('InShopNC') or exit('Access Invalid!');
  8. Class Validate{
  9. /**
  10. * 存放验证信息
  11. *
  12. * @var array
  13. */
  14. public $validateparam = array();
  15. /**
  16. * 验证规则
  17. *
  18. * @var array
  19. */
  20. private $validator = array(
  21. "email"=>'/^([.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.[a-zA-Z0-9_-])+/',
  22. "phone"=>'/^(([0-9]{2,3})|([0-9]{3}-))?((0[0-9]{2,3})|0[0-9]{2,3}-)?[1-9][0-9]{6,7}(-[0-9]{1,4})?$/',
  23. "mobile"=>'/^1[0-9]{10}$/',
  24. "url"=>'/^http:(\\/){2}[A-Za-z0-9]+.[A-Za-z0-9]+[\\/=?%-&_~`@\\[\\]\':+!]*([^<>\"\"])*$/',
  25. "currency"=>'/^[0-9]+(\\.[0-9]+)?$/',
  26. "number"=>'/^[0-9]+$/',
  27. "zip"=>'/^[0-9][0-9]{5}$/',
  28. "qq"=>'/^[1-9][0-9]{4,8}$/',
  29. "integer"=>'/^[-+]?[0-9]+$/',
  30. "integerpositive"=>'/^[+]?[0-9]+$/',
  31. "double"=>'/^[-+]?[0-9]+(\\.[0-9]+)?$/',
  32. "doublepositive"=>'/^[+]?[0-9]+(\\.[0-9]+)?$/',
  33. "english"=>'/^[A-Za-z]+$/',
  34. "chinese"=>'/^[\x80-\xff]+$/',
  35. "username"=>'/^[\\w]{3,}$/',
  36. "nochinese"=>'/^[A-Za-z0-9_-]+$/',
  37. );
  38. /**
  39. * 验证数组中的值
  40. *
  41. * <code>
  42. * //使用示例
  43. * <?php
  44. * require("commonvalidate.class.php");
  45. * $a = new CommonValidate();
  46. * $a->setValidate("344d",true,"","不可以为空");
  47. * $a->setValidate("fdsfsfd",true,"Email","请填写正确的EMAIL");
  48. * echo $a->validate();
  49. *
  50. * //显示结果:
  51. * 请填写正确的EMAIL
  52. * ? >
  53. * </code>
  54. *
  55. * @param
  56. * @return string 字符串类型的返回结果
  57. */
  58. public function validate(){
  59. if (!is_array($this->validateparam)){
  60. return false;
  61. }
  62. foreach($this->validateparam as $k=>$v){
  63. $v['validator'] = strtolower($v['validator']);
  64. if ($v['require'] == ""){
  65. $v['require'] = false;
  66. }
  67. if ($v['input'] == "" && $v['require'] == "true"){
  68. $this->validateparam[$k]['result'] = false;
  69. }else{
  70. $this->validateparam[$k]['result'] = true;
  71. }
  72. if ($this->validateparam[$k]['result'] && $v['input'] != ""){
  73. switch($v['validator']){
  74. case "custom":
  75. $this->validateparam[$k]['result'] = $this->check($v['input'],$v['regexp']);
  76. break;
  77. case "compare":
  78. if ($v['operator'] != ""){
  79. eval("\$result = '" . $v['input'] . "'" . $v['operator'] . "'" . $v['to'] . "'" . ";" );
  80. $this->validateparam[$k]['result'] = $result;
  81. }
  82. break;
  83. case "length":
  84. //判断编码取字符串长度
  85. $input_encode = mb_detect_encoding($v['input'],array('UTF-8','GBK','ASCII',));
  86. $input_length = mb_strlen($v['input'],$input_encode);
  87. if (intval($v['min']) >= 0 && intval($v['max']) > intval($v['min'])){
  88. $this->validateparam[$k]['result'] = ($input_length >= intval($v['min']) && $input_length <= intval($v['max']));
  89. }
  90. else if (intval($v['min']) >= 0 && intval($v['max']) <= intval($v['min'])){
  91. $this->validateparam[$k]['result'] = ($input_length == intval($v['min']));
  92. }
  93. break;
  94. case "range":
  95. if (intval($v['min']) >= 0 && intval($v['max']) > intval($v['min'])){
  96. $this->validateparam[$k]['result'] = (intval($v['input']) >= intval($v['min']) && intval($v['input']) <= intval($v['max']));
  97. }
  98. else if (intval($v['min']) >= 0 && intval($v['max']) <= intval($v['min'])){
  99. $this->validateparam[$k]['result'] = (intval($v['input']) == intval($v['min']));
  100. }
  101. break;
  102. default:
  103. $this->validateparam[$k]['result'] = $this->check($v['input'],$this->validator[$v['validator']]);
  104. }
  105. }
  106. }
  107. $error = $this->getError();
  108. $this->validateparam = array();
  109. return $error;
  110. }
  111. /**
  112. * 正则表达式运算
  113. *
  114. * @param string $str 验证字符串
  115. * @param string $validator 验证规则
  116. * @return bool 布尔类型的返回结果
  117. */
  118. private function check($str='',$validator=''){
  119. if ($str != "" && $validator != ""){
  120. if (preg_match($validator,$str)){
  121. return true;
  122. }
  123. else{
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. /**
  130. * 需要验证的内容
  131. *
  132. * @param array $validateparam array("input"=>"","require"=>"","validator"=>"","regexp"=>"","operator"=>"","to"=>"","min"=>"","max"=>"",message=>"")
  133. * input要验证的值
  134. * require是否必填,true是必填false是可选
  135. * validator验证的类型:
  136. * 其中Compare,Custom,Length,Range比较特殊。
  137. * Compare是用来比较2个字符串或数字,operator和to用来配合使用,operator是比较的操作符(==,>,<,>=,<=,!=),to是用来比较的字符串;
  138. * Custom是定制验证的规则,regexp用来配合使用,regexp是正则表达试;
  139. * Length是验证字符串或数字的长度是否在一顶的范围内,min和max用来配合使用,min是最小的长度,max是最大的长度,如果不写max则被认为是长度必须等于min;
  140. * Range是数字是否在某个范围内,min和max用来配合使用。
  141. * 值得注意的是,如果需要判断的规则比较复杂,建议直接写正则表达式。
  142. *
  143. * @return void
  144. */
  145. public function setValidate($validateparam){
  146. $validateparam["result"] = true;
  147. $this->validateparam = array_merge($this->validateparam,array($validateparam));
  148. }
  149. /**
  150. * 得到验证的错误信息
  151. *
  152. * @param
  153. * @return string 字符串类型的返回结果
  154. */
  155. private function getError(){
  156. foreach($this->validateparam as $k=>$v){
  157. if ($v['result'] == false){
  158. return $v['message'];
  159. }
  160. }
  161. return null;
  162. }
  163. }
  164. ?>