Jump to content

suggestion for improved email regex


alexweber15

Recommended Posts

nice addition to the forums btw! :)

 

just browsing through the "common expressions" and here's my uber email checking regex:

 

mine:

"^[a-z0-9]+[a-z0-9\?\.\+-_]*"."@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.[a-z]+$"

 

yours:

"[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"

 

comparing them briefly side-by-side mine is more flexible in that it allows for dot dot domains (.com.br for brazil for example) and also enforces starting and ending rules

 

i've used it for ages (adapted from an example someone showed me) and one thing i don't understand is the concatenation in the middle (but it doesnt work without it)

 

also off the top of my head im not sure whether or not question marks (allowed in mine) and ampersands (not allowed in mine) are allowed or whether email addresses are allowed to start with underscores for example (too lazy to read up on this at the moment)

 

I don't mean to be a dick or anything just throwing my idea out there to maybe help the community! :)

 

Alex

Link to comment
Share on other sites

Doesn't php have a filter email validator function?

 

in fact it does! (kind of)

sanitize email filter

but it doesn't actually validate whether the email is well-formed, just cleans it ("sanitizes").

 

and effigy i'm gonna have to get back to you on the example of it failing without the concatenation but i remember it happened once a long time ago and i never tried to remove it again!

Link to comment
Share on other sites

I suggest you check out this article. I'm not sure how accurate the article is but I ran some of the tests it mentioned on your expression.

 

$tests = array(
				array("{^c\@**Dog^}@cartoon.com", 1), #Should pass
				array("example@something.com", 1), #Should pass
				array("dclo@us.ibm.com", 1), #Should pass
				array("_somename@example.com", 1), #Should pass
				array("abc\\@example.com", 0), #Should fail
				array("customer/department=shipping@example.com", 1), #Should pass
				array("Joe.\\\\Blow@example.com", 1), #Should pass
				array("abc@def@example.com", 0), #Should fail
				array("!def!xyz%abc@example.com", 1), #Should pass
				array("dot.@example.com", 0), #Should fail
				);

foreach($tests as $t){
if(preg_match("#^[a-z0-9]+[a-z0-9\?\.\+-_]*"."@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.[a-z]+$#", $t[0])){
	echo "{$t[0]} <b>passed</b>, it should've <b>". ($t[1] == 1 ? "passed" : "failed") . "</b>.<br />";
}else{
	echo "{$t[0]} <b>failed</b>, it should've <b>". ($t[1] == 1 ? "passed" : "failed") . "</b>.<br />";
}
}

 

Output:

{^c\@**Dog^}@cartoon.com failed, it should've passed.
example@something.com passed, it should've passed.
dclo@us.ibm.com passed, it should've passed.
_somename@example.com failed, it should've passed.
abc\@example.com passed, it should've failed.
customer/department=shipping@example.com passed, it should've passed.
Joe.\\Blow@example.com failed, it should've passed.
abc@def@example.com passed, it should've failed.
!def!xyz%abc@example.com failed, it should've passed.
dot.@example.com passed, it should've failed.

Link to comment
Share on other sites

Doesn't php have a filter email validator function?

 

in fact it does! (kind of)

sanitize email filter

but it doesn't actually validate whether the email is well-formed, just cleans it ("sanitizes").

 

and effigy i'm gonna have to get back to you on the example of it failing without the concatenation but i remember it happened once a long time ago and i never tried to remove it again!

 

No no, it also has a function to validate the email -> http://www.w3schools.com/PHP/filter_validate_email.asp

Link to comment
Share on other sites

I suggest you check out this article. I'm not sure how accurate the article is but I ran some of the tests it mentioned on your expression.

 

$tests = array(
				array("{^c\@**Dog^}@cartoon.com", 1), #Should pass
				array("example@something.com", 1), #Should pass
				array("dclo@us.ibm.com", 1), #Should pass
				array("_somename@example.com", 1), #Should pass
				array("abc\\@example.com", 0), #Should fail
				array("customer/department=shipping@example.com", 1), #Should pass
				array("Joe.\\\\Blow@example.com", 1), #Should pass
				array("abc@def@example.com", 0), #Should fail
				array("!def!xyz%abc@example.com", 1), #Should pass
				array("dot.@example.com", 0), #Should fail
				);

foreach($tests as $t){
if(preg_match("#^[a-z0-9]+[a-z0-9\?\.\+-_]*"."@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.[a-z]+$#", $t[0])){
	echo "{$t[0]} <b>passed</b>, it should've <b>". ($t[1] == 1 ? "passed" : "failed") . "</b>.<br />";
}else{
	echo "{$t[0]} <b>failed</b>, it should've <b>". ($t[1] == 1 ? "passed" : "failed") . "</b>.<br />";
}
}

 

Output:

{^c\@**Dog^}@cartoon.com failed, it should've passed.
example@something.com passed, it should've passed.
dclo@us.ibm.com passed, it should've passed.
_somename@example.com failed, it should've passed.
abc\@example.com passed, it should've failed.
customer/department=shipping@example.com passed, it should've passed.
Joe.\\Blow@example.com failed, it should've passed.
abc@def@example.com passed, it should've failed.
!def!xyz%abc@example.com failed, it should've passed.
dot.@example.com passed, it should've failed.

 

 

thanks for the code and thanks Acs for the link :)

 

gonna run some tests and revisit my standard function :)

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.