A good PHP validation class

Source: Internet
Author: User
  1. /**
  2. * Validation Class
  3. *
  4. * @lastmodify 2014-5-16
  5. * @author jy625
  6. */
  7. Class verifyaction{
  8. /**
  9. * is a null value
  10. */
  11. public static function IsEmpty ($STR) {
  12. $str = Trim ($STR);
  13. Return!empty ($STR)? True:false;
  14. }
  15. /**
  16. * Digital Verification
  17. * param: $flag: int is an integer, float is floating point type
  18. */
  19. public static function Isnum ($str, $flag = ' float ') {
  20. if (!self::isempty ($STR)) return false;
  21. if (Strtolower ($flag) = = = ' int ') {
  22. Return (string) (int) $str = = = (string) $str)? True:false;
  23. }else{
  24. Return (string) (float) $str = = = (string) $str)? True:false;
  25. }
  26. }
  27. /**
  28. * Name matching, such as user name, directory name, etc.
  29. * @param: String $str strings to match
  30. * @param: Whether $chinese support Chinese, default support, if it is a matching file name, it is recommended to turn off this item (FALSE)
  31. * @param: $charset encoding (default utf-8, support gb2312)
  32. */
  33. public static function Isname ($STR, $chinese = true, $charset = ' utf-8 ') {
  34. if (!self::isempty ($STR)) return false;
  35. if ($chinese) {
  36. $match = (Strtolower ($charset) = = ' gb2312 ')? "/^[". Chr (0XA1). " -". Chr (0xff)." a-za-z0-9_-]+$/":"/^[x{4e00}-x{9fa5}a-za-z0-9_]+$/u ";
  37. }else{
  38. $match = '/^[a-za-z0-9_-]+$/';
  39. }
  40. Return Preg_match ($match, $str)? True:false;
  41. }
  42. /**
  43. * Email Verification
  44. */
  45. public static function Isemail ($STR) {
  46. if (!self::isempty ($STR)) return false;
  47. Return Preg_match ("/([a-z0-9]*[-_\.]? [a-z0-9]+] *@ ([a-z0-9]*[-_]?[ a-z0-9]+) +[\.] [A-z] {2,3} ([\.] [A-z] {2})? /i ", $str)? True:false;
  48. }
  49. Mobile phone number Verification
  50. public static function IsMobile ($STR) {
  51. $exp = "/^13[0-9] [0-9]{8}$|15[012356789] small shellfish [0-9]{8}$|18[012356789] small shellfish [0-9]{8}$|14[57] small shellfish [0-9]$/];
  52. if (Preg_match ($exp, $str)) {
  53. return true;
  54. }else{
  55. return false;
  56. }
  57. }
  58. /**
  59. * URL authentication, plain URL format, IP authentication not supported
  60. */
  61. public static function Isurl ($STR) {
  62. if (!self::isempty ($STR)) return false;
  63. return Preg_match (' # (HTTP|HTTPS|FTP|FTPS)://([w-]+.) +[w-]+ (/[w-./?%&=]*)? #i ', $str)? True:false;
  64. }
  65. /**
  66. * Verify Chinese
  67. * @param: String $str strings to match
  68. * @param: $charset encoding (default utf-8, support gb2312)
  69. */
  70. public static function Ischinese ($str, $charset = ' utf-8 ') {
  71. if (!self::isempty ($STR)) return false;
  72. $match = (Strtolower ($charset) = = ' gb2312 ')? "/^[". Chr (0XA1). " -". Chr (0xff)." +$/"
  73. : "/^[x{4e00}-x{9fa5}]+$/u";
  74. Return Preg_match ($match, $str)? True:false;
  75. }
  76. /**
  77. * UTF-8 Verification
  78. */
  79. public static function IsUtf8 ($STR) {
  80. if (!self::isempty ($STR)) return false;
  81. Return (Preg_match ("/^ ([". chr (228). " -". Chr (233)." Beckham [". chr (128)." -". Chr (191)." Beckham [". chr (128)." -". Chr (191)." Beckham) Beckham/", $word)
  82. = = True | | Preg_match ("/[". chr (228). " -". Chr (233)." Beckham [". chr (128)." -". Chr (191)." Beckham [". chr (128)." -". Chr (191)." Beckham) $/", $word)
  83. = = True | | Preg_match ("/[". chr (228). " -". Chr (233)." Beckham [". chr (128)." -". Chr (191)." Beckham [". chr (128)." -". Chr (191)." Beckham) {2,}/", $word)
  84. = = True)? True:false;
  85. }
  86. /**
  87. * Verify Length
  88. * @param: String $str
  89. * @param: int $type (mode, default min <= $str <= max)
  90. * @param: int $min, Min., $max, maximum;
  91. * @param: String $charset character
  92. */
  93. public static function Length ($STR, $type =3, $min =0, $max =0, $charset = ' utf-8 ') {
  94. if (!self::isempty ($STR)) return false;
  95. $len = Mb_strlen ($str, $charset);
  96. Switch ($type) {
  97. Case 1://Match Minimum value only
  98. Return ($len >= $min)? True:false;
  99. Break
  100. Case 2://Match Max value only
  101. Return ($max >= $len)? True:false;
  102. Break
  103. Default://min <= $str <= Max
  104. Return (($min <= $len) && ($len <= $max))? True:false;
  105. }
  106. }
  107. /**
  108. * Verify Password
  109. * @param string $value
  110. * @param int $length
  111. * @return Boolean
  112. */
  113. public static function Ispwd ($value, $minLen =6, $maxLen =16) {
  114. $match = '/^[\\~!@#$%^&* ()-_=+|{}
  115. ,.? \/:;\ ' \d\w]{'. $minLen. ', '. $maxLen. '} $/';
  116. $v = Trim ($value);
  117. if (empty ($v))
  118. return false;
  119. Return Preg_match ($match, $v);
  120. }
  121. /**
  122. * Verify user name
  123. * @param string $value
  124. * @param int $length
  125. * @return Boolean
  126. */
  127. public static function Isnames ($value, $minLen =2, $maxLen =16, $charset = ' all ') {
  128. if (empty ($value))
  129. return false;
  130. Switch ($charset) {
  131. Case ' EN ': $match = '/^[_\w\d]{'. $minLen. ', '. $maxLen. '} $/iu ';
  132. Break
  133. Case ' CN ': $match = '/^[_\x{4e00}-\x{9fa5}\d]{'. $minLen. ', '. $maxLen. '} $/iu ';
  134. Break
  135. Default: $match = '/^[_\w\d\x{4e00}-\x{9fa5}]{'. $minLen. ', '. $maxLen. '} $/iu ';
  136. }
  137. Return Preg_match ($match, $value);
  138. }
  139. /**
  140. * Verify Mailbox
  141. * @param string $value
  142. */
  143. public static function Checkzip ($STR) {
  144. if (strlen ($STR)!=6) {
  145. return false;
  146. }
  147. if (substr ($str, 0, 1) ==0) {
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * Match Date
  154. * @param string $value
  155. */
  156. public static function Checkdate ($STR) {
  157. $DATEARR = Explode ("-", $str);
  158. if (is_numeric ($dateArr [0]) && is_numeric ($DATEARR [1]) && is_numeric ($DATEARR [2])) {
  159. if ($dateArr [0] >= && $timeArr [0] <= 10000) && ($DATEARR [1] >= 0 && $dateArr [1] < && ($DATEARR [2] >= 0 && $dateArr [2] <= 31))
  160. return true;
  161. Else
  162. return false;
  163. }
  164. return false;
  165. }
  166. /**
  167. * Match Time
  168. * @param string $value
  169. */
  170. public static function Checktime ($STR) {
  171. $TIMEARR = Explode (":", $STR);
  172. if (is_numeric ($timeArr [0]) && is_numeric ($TIMEARR [1]) && is_numeric ($TIMEARR [2])) {
  173. if ($timeArr [0] >= 0 && $timeArr [0] <= && ($TIMEARR [1] >= 0 && $timeArr [1] <= 59) && ($TIMEARR [2] >= 0 && $timeArr [2] <= 59))
  174. return true;
  175. Else
  176. return false;
  177. }
  178. return false;
  179. }
  180. }
Copy Code
Very good, PHP
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.