Forum Moderators: coopster

Message Too Old, No Replies

Email validator rejecting emails with dashes?

can't figure out what to change

         

Doood

10:07 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



My contact form gives an error message when you enter an email with dashes in it, but I don't know why.

I removed the dashes from the code I thought were the culprit but it's still rejecting dashes. Look at the code and see if you can find it.

Here's the original code,

if(ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.) ¦(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}¦[0-9]{1,3})(\]?)$", $var)) { 
return TRUE;
} else {
return FALSE;
}

and so I removed the dashes "-" that I thought would fix it but didn't.

Here's what it looks like now,

if(ereg("^([a-zA-Z0-9_\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.) ¦(([a-zA-Z0-9]+\.)+))([a-zA-Z]{2,4}¦[0-9]{1,3})(\]?)$", $var)) { 
return TRUE;
} else {
return FALSE;
}

Can you find it? Am I missing the obvious?

[edited by: dreamcatcher at 9:09 am (utc) on Dec. 21, 2007]
[edit reason] fixed side scroll [/edit]

omoutop

6:54 am on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



perhaps this will do the trick?

preg_match('/^[a-z0-9.+_-]+@([a-z0-9-]+.)+[a-z]+$/i', $_POST['email'])

henry0

1:01 pm on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or maybe that one :)

<?php
function validate_email($email)
{
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

// Presume that the email is invalid
$valid = 0;

// Validate the syntax
if (eregi($regexp, $email))
{
list($username,$domaintld) = split("@",$email);

// Validate the domain
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
} else {
$valid = 0;
}

return $valid;

}
?>

Keep adding suggestions and comments....

justgowithit

2:44 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().

omoutop

3:07 pm on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



imho:

true if you want to use regular expressions on a large volume of data (scan the pages of a book and replace a name instance for another one, for example).

in case of validating a single email adress, even if you force the server to perform some thousants such checks, you wont notice a significant improvement on speed.

Either way, this is a good note to keep in mind.

henry0

3:26 pm on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use mine for validating new registrant
I guess I should wish that site to have thousands of new users per hour :)

Doood

7:10 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



I kind of don't want to rewrite the whole script cause it's being used in about a dozen or so locations.

I'm thinking now that dashes just need to be added to the ereg part so they will be allowed.

justgowithit

7:52 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



I try to squeeze as much optimization out of my scripts as possible. If there's an instance where I can increase performance by .001 second and there are thousands of such instances throughout the structure of an application I feel the attention to detail is justified. :)