jasonc Posted September 29, 2010 Share Posted September 29, 2010 the following script is not allowing email addresses which have something@360.com the @360.com part is what is failing. can anyone suggest a better email validation script, one that checks very basic tests to see of the email address has the @ and a . (dot) as this script i have been using seems to be failing me now. function ValidEmail($address) { //echo('.'.$address.'.'); $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // allowed characters for part before "at" character $domain = '([a-z]([-a-z0-9]*[a-z0-9]+)?)'; // allowed characters for part after "at" character $regex = '^' . $atom . '+' . // One or more atom characters. '(\.' . $atom . '+)*'. // Followed by zero or more dot separated sets of one or more atom characters. '@'. // Followed by an "at" character. '(' . $domain . '{1,63}\.)+'. // Followed by one or max 63 domain characters (dot separated). $domain . '{2,63}'. // Must be followed by one set consisting a period of two '$'; // or max 63 domain characters. if ($address =="") { $address = ""; return false; } elseif (!eregi($regex, $address)) { $address = "invalid"; return false; } return true; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 29, 2010 Share Posted September 29, 2010 Here is the one I have developed and continued to improve along with a complete explanation of how it does the validation. // NOTES: // // Format test // - Username: // - Can contain the following characters: // - Uppercase and lowercase English letters (a-z, A-Z) // - Digits 0 to 9 // - Characters _ ! # $ % & ' * + - / = ? ^ ` { | } ~ // - May contain '.' (periods), but cannot begin or end with a period // and they may not appear in succession (i.e. 2 or more in a row) // - Must be between 1 and 64 characters // - Domain: // - Can contain the following characters: 'a-z', 'A-Z', '0-9', '-' (hyphen), and '.' (period). // - There may be subdomains, separated by a period (.), but the combined domain may not // begin with a period and they not appear in succession (i.e. 2 or more in a row) // - Domain/Subdomain name parts may not begin or end with a hyphen // - Domain/Subdomain name parts must be between 1-64 characters // - TLD accepts: 'a-z' & 'A-Z' (2 to 6 characters) // // Note: the domain and tld parts must be between 4 and 255 characters total // // Length test // - Username: 1 to 64 characters // - Domain: 4 to 255 character //===================================================== // Function: is_email ( string $email ) // // Description: Finds whether the given string variable // is a properly formatted email. // // Parameters: $email the string being evaluated // // Return Values: Returns TRUE if $email is valid email // format, FALSE otherwise. //===================================================== function is_email($email) { $formatTest = '/^[\w!#$%&\'*+\-\/=?^`{|}~]+(\.[\w!#$%&\'*+\-\/=?^`{|}~]+)*@[a-z\d]([a-z\d-]{0,62}[a-z\d])?(\.[a-z\d]([a-z\d-]{0,62}[a-z\d])?)*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); } Quote Link to comment Share on other sites More sharing options...
bob.newtek Posted September 30, 2010 Share Posted September 30, 2010 The check errantly assumes that the domain cannot begin with a number. Try replacing the $domain assignment with this: $domain = '(([-a-z0-9]*[a-z0-9]+)?)'; // allowed characters for part after "at" character and it should work. On a side-note, POSIX-style regular expressions (those using the ereg_*() functions) are deprecated in PHP as of v5.3. You might want to think about switching to the PCRE functions (preg_match() in this case). Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 30, 2010 Share Posted September 30, 2010 Also, if you are using PHP 5.2.0 or later you might want to try using the filter_var() function with the FILTER_VALIDATE_EMAIL flag. http://us.php.net/manual/en/filter.examples.validation.php I have not used it myself to test how comprehensive it is. Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 30, 2010 Share Posted September 30, 2010 Also, if you are using PHP 5.2.0 or later you might want to try using the filter_var() function with the FILTER_VALIDATE_EMAIL flag. http://us.php.net/manual/en/filter.examples.validation.php I have not used it myself to test how comprehensive it is. Good point, thx for sharing your detailed code also. Cheers! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.