1. <?php
    2. namespace Topxia\Common;
    3. /**
    4. * 一个简单的验证类
    5. */
    6. class SimpleValidator
    7. {
    8. public static function email($value)
    9. {
    10. $value = (string) $value;
    11. $valid = filter_var($value, FILTER_VALIDATE_EMAIL);
    12. return $valid !== false;
    13. }
    14. public static function nickname($value, array $option = array())
    15. {
    16. $option = array_merge(
    17. array('minLength' => 4, 'maxLength' => 18),
    18. $option
    19. );
    20. $len = (strlen($value) + mb_strlen($value, 'utf-8')) / 2;
    21. if ($len > $option['maxLength'] || $len < $option['minLength']) {
    22. return false;
    23. }
    24. if (preg_match('/^1\d{10}$/', $value)) {
    25. return false;
    26. }
    27. return !!preg_match('/^[\x{4e00}-\x{9fa5}a-zA-z0-9_.·]+$/u', $value);
    28. }
    29. public static function password($value, array $option = array())
    30. {
    31. return !!preg_match('/^[\S]{5,20}$/u', $value);
    32. }
    33. //真实姓名改成和nickname一样
    34. public static function truename($value, array $option = array())
    35. {
    36. $option = array_merge(
    37. array('minLength' => 4, 'maxLength' => 18),
    38. $option
    39. );
    40. $len = (strlen($value) + mb_strlen($value, 'utf-8')) / 2;
    41. if ($len > $option['maxLength'] || $len < $option['minLength']) {
    42. return false;
    43. }
    44. if (preg_match('/^1\d{10}$/', $value)) {
    45. return false;
    46. }
    47. return !!preg_match('/^[\x{4e00}-\x{9fa5}a-zA-z_.·]+$/u', $value);
    48. }
    49. public static function idcard($value)
    50. {
    51. return !!preg_match('/^\d{17}[0-9xX]$/', $value);
    52. }
    53. public static function bankCardId($value)
    54. {
    55. return !!preg_match('/^(\d{16}|\d{19})$/', $value);
    56. }
    57. public static function mobile($value)
    58. {
    59. return !!preg_match('/^1\d{10}$/', $value);
    60. }
    61. public static function numbers($value)
    62. {
    63. return !!preg_match('/^(\d+,?)*\d+$/', $value);
    64. }
    65. public static function phone($value)
    66. {
    67. return !!preg_match('/^(\d{4}-|\d{3}-)?(\d{8}|\d{7})$/', $value);
    68. }
    69. public static function date($value)
    70. {
    71. return !!preg_match('/^(\d{4}|\d{2})-((0?([1-9]))|(1[0-2]))-((0?[1-9])|([12]([0-9]))|(3[0|1]))$/', $value);
    72. }
    73. public static function qq($value)
    74. {
    75. return !!preg_match('/^[1-9]\d{4,}$/', $value);
    76. }
    77. public static function integer($value)
    78. {
    79. return !!preg_match('/^[+-]?\d{1,9}$/', $value);
    80. }
    81. public static function float($value)
    82. {
    83. return !!preg_match('/^(([+-]?[1-9]{1}\d*)|([+-]?[0]{1}))(\.(\d){1,2})?$/i', $value);
    84. }
    85. public static function dateTime($value)
    86. {
    87. return !!preg_match('/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/', $value);
    88. }
    89. public static function site($value)
    90. {
    91. return !!preg_match('/^(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/', $value);
    92. }
    93. public static function chineseAndAlphanumeric($value)
    94. {
    95. return (bool)preg_match('/^([\x{4e00}-\x{9fa5}]|[a-zA-Z0-9_.·])*$/u', $value);
    96. }
    97. }