alexweber15 Posted October 17, 2008 Share Posted October 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/ Share on other sites More sharing options...
effigy Posted October 17, 2008 Share Posted October 17, 2008 If you want to match e-mail addresses to a tee, see the RFC. Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/#findComment-668256 Share on other sites More sharing options...
alexweber15 Posted October 17, 2008 Author Share Posted October 17, 2008 If you want to match e-mail addresses to a tee, see the RFC. thanks for the link, will look it up later no comments on my suggestion though? Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/#findComment-668268 Share on other sites More sharing options...
effigy Posted October 17, 2008 Share Posted October 17, 2008 It should be fine without the concatenation. Do you have an example of it failing? Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/#findComment-668269 Share on other sites More sharing options...
Acs Posted October 17, 2008 Share Posted October 17, 2008 Doesn't php have a filter email validator function? Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/#findComment-668284 Share on other sites More sharing options...
alexweber15 Posted October 19, 2008 Author Share Posted October 19, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/#findComment-668909 Share on other sites More sharing options...
JasonLewis Posted October 19, 2008 Share Posted October 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/#findComment-668914 Share on other sites More sharing options...
Acs Posted October 19, 2008 Share Posted October 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/#findComment-669084 Share on other sites More sharing options...
alexweber15 Posted October 21, 2008 Author Share Posted October 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/128897-suggestion-for-improved-email-regex/#findComment-670498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.