note 54956 rejected from function.strpos by sean

From: Date: Wed, 20 Jul 2005 19:00:58 +0000
Subject: note 54956 rejected from function.strpos by sean
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-92225@lists.php.net to get a copy of this message
Note Submitter: krydprz at iit dot edu ---- Here's my stab at email address verification/correctness function. The parameter the function takes is an email address such as foo@bar.net. I hope somebody finds it useful. <?php function check_email_integrity($email_str) { $ret_val = FALSE; while(1) { // an email of form a@b.c has a minimum of 5 characters if (strlen($email_str) < 5) break; // this will eliminate all combinations when 2 or more 'at' symbols are together $which = strpos($email_str, "@@"); if ($which !== FALSE) break; // this will eliminate all combinations when 2 or more dots are together $which = strpos($email_str, ".."); if ($which !== FALSE) break; $which = strpos($email_str, ".@"); if ($which !== FALSE) break; $which = strpos($email_str, "@."); if ($which !== FALSE) break; $which = strpos($email_str, "@"); if ($which === FALSE || $which == 0 || $which > strlen($email_str) - 4) break; $which = strpos($email_str, "."); if ($which === FALSE || $which == 0 || $which == strlen($email_str) - 1) break; $which = strpos(strrev($email_str), "@"); if ($which === FALSE || $which == 0 || $which > strlen($email_str) - 4) break; $which = strpos(strrev($email_str), "."); if ($which === FALSE || $which == 0 || $which == strlen($email_str) - 1) break; $prefix_suffix = explode("@", $email_str); // check if perhaps more than 2 array elements result which would mean // that more than one '@' exists in the string if (count($prefix_suffix) != 2) break; if (strlen($prefix_suffix[1]) < 3) break; $which = strpos($prefix_suffix[1], "."); if ($which === FALSE || $which == 0 || $which == strlen($email_str) - 1) break; // we have a valid email address finally $ret_val = TRUE; break; } return $ret_val; } ?> If someone finds it useful, here's the same thing in Javascript: function verify_email(email_to_verify) { var return_val = false; var email_str = email_to_verify; while(1) { if (email_str.length < 5) break; var which = email_str.indexOf("@@"); if (which != -1) break; which = email_str.indexOf(".."); if (which != -1) break; var which = email_str.indexOf(".@"); if (which != -1) break; which = email_str.indexOf("@."); if (which != -1) break; which = email_str.indexOf("@"); if (which == -1 || which == 0 || which > email_str.length - 4) break; which = email_str.indexOf("."); if (which == -1 || which == 0 || which == email_str.length - 1) break; which = email_str.lastIndexOf("@"); if (which == -1 || which == 0 || which > email_str.length - 4) break; which = email_str.lastIndexOf("."); if (which == -1 || which == 0 || which == email_str.length - 1) break; var prefix_suffix = email_str.split("@"); // check if perhaps more than 2 array elements result which would mean // that more than one '@' exists in the string if (prefix_suffix.length != 2) break; if (prefix_suffix[1].length < 3) break; which = prefix_suffix[1].indexOf("."); if (which == -1 || which == 0 || which == email_str.length - 1) break; // we have a valid email address finally return_val = true; break; } if (return_val == false) alert ("You must provide a valid email address."); return return_val; } Kind Regards, Przemek

« previous php.notes (#92225) next »