Jump to content

this email validation is throwing an error


simcoweb

Recommended Posts

This email field validation is showing the error message that the email is not valid. I can't see where/why it would decline to validate this email address:

 

bozotheclown@hotmail.com

 

Here's the code:

 

// check to make sure email is valid format
if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) {
	$errors[] = '<font color="red">Please provide a valid email address.</font>';
}

Link to comment
Share on other sites

This part...

(\.[a-zA-Z0-9]+){1,2}

 

...should be:

(\.[a-zA-Z0-9]+){1,4}

 

In your regex the last part of the e-mail address - .com .net .org .dk .co.uk - has to be between 1 and 2 characters long, _including_ the . (dot). This means the tld of your domain name has to be 1 character long. No such tld's exists :)

 

So I would recommend that you make it between 1 and 4 characters, since this will leave 3 characters to comprise the tld... this will make most tlds valid.

 

** I'll attend to this again in the morning if you haven't found a solution, but right now I'm too tired and what I've said above might not be 100% correct... but I'm sure it has something to do with your quantities... **

 

 

Link to comment
Share on other sites

Hey, thanks for the post. And the description! It helps to learn it.

 

Unfortunately it's still throwing an error even with that modification. I'm wondering if there's a way to have the error display exactly what is causing the issue so we can narrow down what segment of the regex is not being fulfilled.

 

Anyone?

Link to comment
Share on other sites

To be honest with you, I don't use eregi()/ereg() -  I use use preg_match() instead. You can easily find tutorials, articles, blogs ect. which claims preg_match() is faster and more powerful than ereg()/eregi() - I don't really know if that is true, but it is only good if it is. The reason why I'm telling you this is, that it has a slightly different syntax than ereg()/eregi()... its not a big deal. It almost comes down to adding slashes at the beginning and ending of your regex: "/^[a-z]+$/" instead of "^[a-z]+$".

 

Here is my favorite e-mail regular expression - I wrote it my self but I'm sure it is possible to find identical regexes around the web, after all it's the best ;) (with one exception which I'll get back to)

 

<?PHP

$email = $_POST['email]; //or something like that
$regex = "/^([a-z0-9]+[a-z0-9\._-]*[a-z0-9]+)@([a-z0-9]+[a-z0-9-]*[a-z0-9]+)\.([a-z]{2,3})$/i";

if(preg_match($regex,$email)) {
  echo 'The e-mail address is valid;
}
else {
  echo 'The e-mail address is _not_ valid;
}

?>

 

But back to the regex:

 

^ ensures we start matching from the beginning at the string

([a-z0-9]+[a-z0-9_-]*[a-z0-9]+) one or more occurences of a-z 0-9 followed by any number of occurrences (maybe none) of a-z 0-9 . - _ followed by one or more

occurrences of a-z 0-9

 

@ followed by a @

 

([a-z0-9]+[a-z0-9-]*[a-z0-9]+) followed by one or more occurrences of a-z 0-9 followed by any number of occurrences (maybe non) of a-z 0-9 - followed by one or more occurrences of a-z 0-9

 

\.([a-z]{2,3}) followed by a . (dot) which is escaped with \ (backslash) because it's a special character followed by between 2 and 3 occurences of a-z (matching .dk .com .net ect.)

 

$ ensures we match all the way to the end of the string

 

/i makes the match incasesensetive... so instead of doing [a-zA-Z] all the time you can just do [a-z] and add /i at the end

 

 

I mentioned that it was the best regex appart from one thing and thats the last part matching tlds (.com .net .dk ect.). Lots of domains wont match 2-3 letters... take .co.uk for example or .museum. Therefore I always do this:

 

 

"/^([a-z0-9]+[a-z0-9\._-]*[a-z0-9]+)@([a-z0-9]+[a-z0-9-]*[a-z0-9]+)\.(dk|com|net|org|co\.uk)$/i";

 

This ensures that for example .co.uk can pass your validation. It has the downside that for example .lt or what ever wont because you haven't added it... but if you wanna be 100% sure you can make a list of all tlds in the world - it takes about 10minutes :)

 

 

Unfortunately I'm in a hurry and haven't got time to explain what was wrong with your own regex, but maybe I'll get back to that later.

 

Btw check this place out: http://www.regular-expressions.info/reference.html it's really good for regex references!

 

Best regards

Wuhtzu

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.