PDA

View Full Version : Validating an e-mail address


vinyl-junkie
11-05-2005, 11:02 PM
Here's a slick way of validating an email address that someone passes you through your contact form:
if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $from)) {
// code to send e-mail goes here
}

marek_mar
11-05-2005, 11:48 PM
I'd change a few things.

$email = trim($email);
if (preg_match('/^[a-z0-9._-]+@[a-z0-9._-]+\.([a-z]{2,4})$/i', $email)) {
// code to send e-mail goes here
}

whizard
11-12-2005, 04:16 AM
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

vinyl-junkie
11-12-2005, 04:43 AM
a@b.c would beat the script, as would 1@2.3. But, this script is still great at screening out some nonsense submissions.
Nope. The regular expression says that there must be two to four characters after the @ symbol and that they must be alphabetic. I have this code in place in my contact form, and both your examples don't skirt my edits.

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.

Velox Letum
11-12-2005, 04:58 AM
Nope. The regular expression says that there must be two to four characters after the @ symbol and that they must be alphabetic. I have this code in place in my contact form, and both your examples don't skirt my edits.

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.

Actually I believe it's 2-4 chars after the period for the TLD. 1@2.aa would work.

vinyl-junkie
11-12-2005, 05:03 AM
Actually I believe it's 2-4 chars after the period for the TLD. 1@2.aa would work.
Proving once again that I'm terrible about interpreting regular expressions. ;)

I just tested your example, and it does pass the edits in my contact form.

Velox Letum
11-12-2005, 05:11 AM
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:

$email = trim($email);
if (preg_match('/^[a-z0-9._-]+@[a-z0-9._-]+\.([a-z]{2,4})($|\.([a-z]{2,4})$)/i', $email)) {
// code to send e-mail goes here
}

marek_mar
11-12-2005, 12:06 PM
It has worked with .co.uk before. :p

Velox Letum
11-12-2005, 07:39 PM
It doesn't seem like it would, as a period is not allowed and co.uk is five characters. *shrug*

marek_mar
11-12-2005, 08:42 PM
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

gsnedders
11-12-2005, 09:21 PM
It'd probably be best to modify it to comply to RFC 2822 (http://www.faqs.org/rfcs/rfc2822), 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>

Velox Letum
11-12-2005, 10:28 PM
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

Ahh I see. My apologies.

whizard
11-13-2005, 03:05 PM
Nope. The regular expression says that there must be two to four characters after the @ symbol and that they must be alphabetic. I have this code in place in my contact form, and both your examples don't skirt my edits.

Sorry, I misread the code.


Proving once again that I'm terrible about interpreting regular expressions.

Don't feel so bad ;)

Nikolis
11-15-2005, 11:04 PM
This is the expression I use to check against email address submissions...
preg_match("/^(([^<>()[\]\\\\.,;:\s@\"]+(\.[^<>()[\]\\\\.,;:\s@\"]+)*)|(\"([^\"\\\\\r]|(\\\\[\w\W]))*\"))@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([a-z\-0-9áàäçéèêñóòôöüæøå]+\.)+[a-z]{2,}))$/i", $_POST['email_address'])

Bill Posters
11-16-2005, 09:38 AM
It'd probably be best to modify it to comply to RFC 2822 (http://www.faqs.org/rfcs/rfc2822), which is more or less the rules as to what is, and what isn't a valid email address.


Fwiw…

http://www.regexlib.com/REDetails.aspx?regexp_id=711
^((?>[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)>)$

http://www.regexlib.com/REDetails.aspx?regexp_id=26
^([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})(\]?)$

Scrowler
11-20-2005, 03:55 AM
would this work for something like email@address.co.nz?

vinyl-junkie
11-20-2005, 11:03 AM
would this work for something like email@address.co.nz?
I have this validation code in place on my contact form. I just tried it with your hypothetical email address, and it passed the validation.

felgall
11-20-2005, 08:19 PM
What about

me@downtown.museum

remember that museum is a valid top level domain.

How about

me@[124.56.137.21]

Using the ip address in place of the domain name is also valid.

vinyl-junkie
11-20-2005, 08:24 PM
Neither one of those email addresses passes validation using the regular expression we've been discussing in this thread. I just tried it with the contact form on my website, which has that code in it.

ghell
11-21-2005, 08:39 PM
i dont write php but couldnt half of these regular expressions be cut down using \w (or \W, i forget which) instead of a-zA-Z0-9_