php中邮箱email 电话等格式的验证

1.email验证函数,代码如下:

  1. function isValidEmail($email) {
  2. $email = strtolower($email);
  3. if (!preg_match(“/[^@]{1,64}@[^@]{1,255}/”, $email)) {
  4. return false;
  5. }
  6. $email_array = explode(“@”, $email);
  7. $local_array = explode(“.”, $email_array[0]);
  8. $length = sizeof($local_array);
  9. for ($i = 0; $i < $length; $i++) {
  10. if (!preg_match(“@^[a-z0-9_~-][a-z0-9_~.-]{0,63}$@”, $local_array[$i])) {
  11. return false;
  12. }
  13. }
  14. unset($length);
  15. if (!preg_match(“@^[?[0-9.]+]?$@”, $email_array[1])) {
  16. $domain_array = explode(“.”, $email_array[1]);
  17. $length = sizeof($domain_array);
  18. if ($length < 2) {
  19. return false;
  20. }
  21. for ($i = 0; $i < $length; $i++) {
  22. if (!preg_match(“/^(([a-z0-9][a-z0-9-]{0,61}[a-z0-9])|([a-z0-9]+))$/”, $domain_array[$i])) {
  23. return false;
  24. }
  25. }
  26. unset($length);
  27. }
  28. return true;
  29. }

2.验证移动电话是否符合规范,代码如下:

  1. function isValidCellPhone($cellPhone) {
  2. $flag = FALSE;
  3. if (preg_match(‘@^1[3458][0-9]{9}$@’, $cellPhone)) {
  4. $flag = TRUE;
  5. }
  6. return $flag;
  7. }

3.验证电话是否符合规范,代码如下:

  1. function isValidPhone($phone) {
  2. if (preg_match(‘@^1[3458][0-9]{9}$@’, $phone)) {
  3. return true;
  4. }
  5. if (preg_match(‘@((?:(?:(?:00860?|0)(?:10|2d|[3-9]dd))-?)?([2-8]d{6,7}))@’, $phone)){
  6. return true;
  7. }
  8. if (preg_match(‘@^([48]00(?:d{7}|-d{7}|-d{3}-d{4}|-d{4}-d{3}|d-d{3}-d{3}))$@’, $phone)) {
  9. return true;
  10. }
  11. return false;
  12. }