note 54956 added to function.strpos

From: Date: Wed, 20 Jul 2005 18:57:14 +0000
Subject: note 54956 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-92224@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54956 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54956&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54956&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54956&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54956&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54956&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54956&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54956&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#92224) next »