![]() |
Validating an e-mail address
Here's a slick way of validating an email address that someone passes you through your contact form:
Code:
if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $from)) { |
I'd change a few things.
PHP Code:
|
Still, if you use this, understand that it is not infallible.(sp?) All it does is screen out someone from typing "dga;sfgharggnjg" into the email box. a@b.c would beat the script, as would 1@2.3. But, this script is still great at screening out some nonsense submissions.
Dan |
Quote:
What might be interesting is using the Validate PEAR package. I haven't tried that, but it's supposed to somehow validate the domain as well as the email address. |
Quote:
|
Quote:
I just tested your example, and it does pass the edits in my contact form. |
There also needs to be a clause written into the regex to allow an additional period and 2-4 chars for domains such as .co.uk. I wrote the clause below:
PHP Code:
|
It has worked with .co.uk before. :p
|
It doesn't seem like it would, as a period is not allowed and co.uk is five characters. *shrug*
|
It's becouse the .co matches with the "[a-z0-9._-]+" part just after the @.
From: email@site.co.uk it matches like this: [a-z0-9._-]+ => email @ => @ [a-z0-9._-]+ => site.co \. => . ([a-z]{2,4}) => uk |
It'd probably be best to modify it to comply to RFC 2822, which is more or less the rules as to what is, and what isn't a valid email address. Examples are: user@example.com, User <user@example.com>
|
Quote:
|
Quote:
Quote:
|
This is the expression I use to check against email address submissions...
PHP Code:
|
Quote:
Fwiw… http://www.regexlib.com/REDetails.aspx?regexp_id=711 Code:
^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$Code:
^([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})(\]?)$ |
| All times are GMT +1. The time now is 07:20 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.