CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Post a PHP snippet (http://www.codingforums.com/forumdisplay.php?f=41)
-   -   Validating an e-mail address (http://www.codingforums.com/showthread.php?t=71874)

vinyl-junkie 11-05-2005 10:02 PM

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)) {
  // code to send e-mail goes here
}


marek_mar 11-05-2005 10:48 PM

I'd change a few things.
PHP Code:

$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 03: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 03:43 AM

Quote:

Originally Posted by whizard
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 03:58 AM

Quote:

Originally Posted by vinyl-junkie
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 04:03 AM

Quote:

Originally Posted by Velox Letum
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 04: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:

PHP Code:

$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 11:06 AM

It has worked with .co.uk before. :p

Velox Letum 11-12-2005 06: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 07: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 08:21 PM

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>

Velox Letum 11-12-2005 09:28 PM

Quote:

Originally Posted by marek_mar
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 02:05 PM

Quote:

Originally Posted by vinyl-junkie
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.

Quote:

Originally Posted by vinyl-junkie
Proving once again that I'm terrible about interpreting regular expressions.

Don't feel so bad ;)

Nikolis 11-15-2005 10:04 PM

This is the expression I use to check against email address submissions...
PHP Code:

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 08:38 AM

Quote:

Originally Posted by Error 404
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.


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