validate.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. static public function verify_mobile($mobile)
  39. {
  40. return array('input' => $mobile,'validator' => "mobile", 'require' => true,'message' =>"请填写正确的手机号码");
  41. }
  42. static public function verify_password($password)
  43. {
  44. return array('input' => $password,'require' => "true", 'validator'=>'length','min'=>6,'max'=>16,'message' =>"密码不能为空,长度为6到16位.");
  45. }
  46. static public function verify_openid($openid)
  47. {
  48. return array('input' => $openid,'require' => "true", 'validator'=>'length','min'=>6,'max'=>40,'message' =>"开放账号不能为空,长度要大于6位.");
  49. }
  50. static public function notnull($val)
  51. {
  52. return array('input' => $val, 'require' => true,'message' =>"请保持参数非空.");
  53. }
  54. static public function verify_smscode($val)
  55. {
  56. return array('input' => $val,'require' => "true", 'validator'=>'length','min'=>4,'max'=>4,'message' =>"短信验证码长度只能为4.");
  57. }
  58. static public function verify_number($val,$error = '输入的值不是数字类型.')
  59. {
  60. return array('input' => $val,'validator' => "number", 'require' => true,'message' => $error);
  61. }
  62. static public function verify_serialnum($sn)
  63. {
  64. return array('input' => $sn,'validator'=>'length','min'=>16,'max'=>20,'message' =>"SN序列号只能是16位到20位.");
  65. }
  66. /**
  67. * 验证数组中的值
  68. *
  69. * <code>
  70. * //使用示例
  71. * <?php
  72. * require("commonvalidate.class.php");
  73. * $a = new CommonValidate();
  74. * $a->setValidate("344d",true,"","不可以为空");
  75. * $a->setValidate("fdsfsfd",true,"Email","请填写正确的EMAIL");
  76. * echo $a->validate();
  77. *
  78. * //显示结果:
  79. * 请填写正确的EMAIL
  80. * ? >
  81. * </code>
  82. *
  83. * @param
  84. * @return string 字符串类型的返回结果
  85. */
  86. public function validate()
  87. {
  88. if (!is_array($this->validateparam)) {
  89. return false;
  90. }
  91. foreach($this->validateparam as $k=>$v)
  92. {
  93. $v['validator'] = strtolower($v['validator']);
  94. if ($v['require'] == ""){
  95. $v['require'] = false;
  96. }
  97. if ($v['input'] == "" && $v['require'] == "true") {
  98. $this->validateparam[$k]['result'] = false;
  99. } else {
  100. $this->validateparam[$k]['result'] = true;
  101. }
  102. if ($this->validateparam[$k]['result'] && $v['input'] != "")
  103. {
  104. switch($v['validator'])
  105. {
  106. case "custom":
  107. $this->validateparam[$k]['result'] = $this->check($v['input'],$v['regexp']);
  108. break;
  109. case "compare":
  110. if ($v['operator'] != ""){
  111. eval("\$result = '" . $v['input'] . "'" . $v['operator'] . "'" . $v['to'] . "'" . ";" );
  112. $this->validateparam[$k]['result'] = $result;
  113. }
  114. break;
  115. case "length":
  116. //判断编码取字符串长度
  117. $input_encode = mb_detect_encoding($v['input'],array('UTF-8','GBK','ASCII',));
  118. $input_length = mb_strlen($v['input'],$input_encode);
  119. if (intval($v['min']) >= 0 && intval($v['max']) > intval($v['min'])){
  120. $this->validateparam[$k]['result'] = ($input_length >= intval($v['min']) && $input_length <= intval($v['max']));
  121. }
  122. else if (intval($v['min']) >= 0 && intval($v['max']) <= intval($v['min'])){
  123. $this->validateparam[$k]['result'] = ($input_length == intval($v['min']));
  124. }
  125. break;
  126. case "range":
  127. if (intval($v['min']) >= 0 && intval($v['max']) > intval($v['min'])){
  128. $this->validateparam[$k]['result'] = (intval($v['input']) >= intval($v['min']) && intval($v['input']) <= intval($v['max']));
  129. }
  130. else if (intval($v['min']) >= 0 && intval($v['max']) <= intval($v['min'])){
  131. $this->validateparam[$k]['result'] = (intval($v['input']) == intval($v['min']));
  132. }
  133. break;
  134. default:
  135. $this->validateparam[$k]['result'] = $this->check($v['input'],$this->validator[$v['validator']]);
  136. }
  137. }
  138. }
  139. $error = $this->getError();
  140. $this->validateparam = array();
  141. return $error;
  142. }
  143. /**
  144. * 正则表达式运算
  145. *
  146. * @param string $str 验证字符串
  147. * @param string $validator 验证规则
  148. * @return bool 布尔类型的返回结果
  149. */
  150. private function check($str='',$validator='')
  151. {
  152. if ($str != "" && $validator != "")
  153. {
  154. if (preg_match($validator,$str)) {
  155. return true;
  156. }
  157. else{
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. /**
  164. * 需要验证的内容
  165. *
  166. * @param array $validateparam array("input"=>"","require"=>"","validator"=>"","regexp"=>"","operator"=>"","to"=>"","min"=>"","max"=>"",message=>"")
  167. * input要验证的值
  168. * require是否必填,true是必填false是可选
  169. * validator验证的类型:
  170. * 其中Compare,Custom,Length,Range比较特殊。
  171. * Compare是用来比较2个字符串或数字,operator和to用来配合使用,operator是比较的操作符(==,>,<,>=,<=,!=),to是用来比较的字符串;
  172. * Custom是定制验证的规则,regexp用来配合使用,regexp是正则表达试;
  173. * Length是验证字符串或数字的长度是否在一顶的范围内,min和max用来配合使用,min是最小的长度,max是最大的长度,如果不写max则被认为是长度必须等于min;
  174. * Range是数字是否在某个范围内,min和max用来配合使用。
  175. * 值得注意的是,如果需要判断的规则比较复杂,建议直接写正则表达式。
  176. *
  177. * @return void
  178. */
  179. public function setValidate($validateparam)
  180. {
  181. $validateparam['result'] = true;
  182. $this->validateparam = array_merge($this->validateparam,array($validateparam));
  183. }
  184. /**
  185. * 得到验证的错误信息
  186. *
  187. * @param
  188. * @return string 字符串类型的返回结果
  189. */
  190. private function getError()
  191. {
  192. foreach($this->validateparam as $k=>$v){
  193. if ($v['result'] == false){
  194. return $v['message'];
  195. }
  196. }
  197. return null;
  198. }
  199. }
  200. ?>