Form verification-php Tutorial

Source: Internet
Author: User
Form verification
<无详细内容>

  1. /**
  2. * Page function: Common form verification classes
  3. * Prepared by: phper
  4. * Creation Time: 2011-4-11
  5. */
  6. Class validate {
  7. Public static function isEmail ($ str ){
  8. Return is_string ($ str) & preg_match ('/^ [_\. 0-9a-z-] + @ ([0-9a-z] [0-9a-z-] + \.) + [a-z] {2, 4} $/', $ str );
  9. }
  10. Public static function isUrl ($ str ){
  11. Return is_string ($ str) & preg_match ("/^ http: \ // [A-Za-z0-9] + \. [A-Za-z0-9] + [\/= \? % \-&_~ '@ [\] \': +!] * ([^ <> \ "\"]) * $/", $ Str );
  12. }
  13. Public static function isPhone ($ str, $ type ){
  14. $ Preg_array_pho = array (
  15. 'Cn' => '/^ (\ d {3} \) | (\ d {3 }\-))? (\ (0 \ d {2, 3} \) | 0 \ d {2, 3 }-)? [1-9] \ d {6, 7} $ /',
  16. 'Tw '=>''
  17. );
  18. If (in_array ($ type, array_keys ($ pre_array_pho ))){
  19. Return preg_match ($ pre_array_pho [$ type], $ str );
  20. } Else {
  21. Die ($ type. '-phone number is undefined ');
  22. }
  23. }
  24. Public static function isText ($ str, $ type, $ min_lenth = 1, $ max_lenth = ''){
  25. $ Preg_array_text = array (
  26. 'Ch' => "/^ ([\ x81-\ xfe] [\ x40-\ xfe]) {". $ min_lenth. ",". $ max_lenth. "} $ /",
  27. 'Num' => "/^ [0-9] {". $ min_lenth. ",". $ max_lenth. "} $/I ",
  28. );
  29. If (in_array ($ type, array_keys ($ preg_array_text ))){
  30. Return is_string ($ preg_array_text) & preg_match ($ preg_array_text [$ type], $ str );
  31. } Else {
  32. Die ($ type. '-text is undefined ');
  33. }
  34. }
  35. Public static function isIp ($ ip ){
  36. Return preg_match ("/^ ([1-9] | [1-9] [0-9] | 1 [0-9] [0-9] | 2 [0-9- 4] [0-9] | 25 [0-5]) ". "(\. ([0-9] | [1-9] [0-9] | 1 [0-9] [0-9] | 2 [0-4] [0-9] | 25 [0-5]) {3} $/", $ ip );
  37. }
  38. Public static function isDate ($ var ){
  39. Return preg_match ("/([0-9] {4})-([0-9] {1, 2})-([0-9] {1, 2 })/", $ var );
  40. }
  41. Public static function isColor ($ var ){
  42. Return preg_match ("/^ #? ([A-f] | [A-F] | [0-9]) {3} ([a-f] | [A-F] | [0-9]) {3 })? $/", $ Var );
  43. }
  44. Public static function isUserName ($ var ){
  45. Return preg_match ("/^ [a-zA-Z0-9 _ \. \-] {} $/", $ var );
  46. }
  47. Public static function isPic ($ var ){
  48. Return preg_match ("/^ [a-zA-Z0-9 \-\.] + \. (jpg | jpeg | gif | png) $/", $ var );
  49. }
  50. }
  51. ?>

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.