validate.php 6.8 KB

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