| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hello Guys, Does any have a regex for email validation? I need to allow only period and underscore in the local part , we would need a @ and .com or watever for domain. thank you |
|
#2
| |||
| |||
| > On 8/27/08, VamVan <vamseevan@gmail.com> wrote: > Hello Guys, > > Does any have a regex for email validation? I need to allow only period and > underscore in the local part , we would need a @ and .com or watever for > domain. php should have a good check built-in. see http://www.php.net/manual/en/function.filter-var.php if(!filter_var($var, FILTER_VALIDATE_EMAIL)) { echo "invalid email"; } some people also go the extra mile and verify the MX record is valid, or lookup the MX record and even validate the user exists by querying the mail server. |
|
#3
| |||
| |||
| VamVan wrote: > Hello Guys, > > Does any have a regex for email validation? I need to allow only > period and underscore in the local part , we would need a @ and .com > or watever for domain. Option 1: /^[_.]+@[a-z0-9-]+\.com$/ This is probably what you meant: /^[a-z0-9_.]+@[a-z0-9-]+\.com$/ /Per Jessen, Zürich |
|
#4
| |||
| |||
| That's a very handy extension. Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com mike wrote: > > php should have a good check built-in. > > see http://www.php.net/manual/en/function.filter-var.php > > if(!filter_var($var, FILTER_VALIDATE_EMAIL)) { > echo "invalid email"; > } > > some people also go the extra mile and verify the MX record is valid, > or lookup the MX record and even validate the user exists by querying > the mail server. > > |
|
#5
| |||
| |||
| > Does any have a regex for email validation? I need to allow only period and > underscore in the local part , we would need a @ and .com or watever for > domain. You could: 1. Take the isValidInetAddress() method out of the PEAR Mail_RFC822 class and use that. 2. Use the filter extension which I understand is now part of PHP (5.2.x+) This would the preferable option. -- Richard Heyes HTML5 Graphing: http://www.phpguru.org/RGraph |
|
#6
| |||
| |||
| At 9:31 AM +0200 8/27/08, Per Jessen wrote: >VamVan wrote: > >> Hello Guys, >> >> Does any have a regex for email validation? I need to allow only >> period and underscore in the local part , we would need a @ and .com >> or watever for domain. > >Option 1: /^[_.]+@[a-z0-9-]+\.com$/ > >This is probably what you meant: > >/^[a-z0-9_.]+@[a-z0-9-]+\.com$/ > >/Per Jessen, Zürich Which is probably what you meant: eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email) Email comes in different TLD flavors. But, even that still doesn't cover all the possible and legal Unicode code-points that can exist on both sides of the @ of an email address. For example: tedd@ˆ.com is a legal and working email address. Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com |
|
#7
| |||
| |||
| <?php # this one worked fine for me, but it does not cover the full RFC like: "name" name@notworking.cc OR name <name@notworking.cc> $regex = "^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$"; if (eregi($regex, $email)) { // do something } # Beware that the filter functions only work under PHP5+. If your PHP supports them they should be the preferred choice ?> |
|
#8
| |||
| |||
| tedd wrote: >> >>Option 1: /^[_.]+@[a-z0-9-]+\.com$/ >> >>This is probably what you meant: >> >>/^[a-z0-9_.]+@[a-z0-9-]+\.com$/ >> >>/Per Jessen, Zürich > > Which is probably what you meant: > > eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email) > > Email comes in different TLD flavors. Well, I left that for the OP to figure out. Still, your regex is worse - a domain name cannot contain '%'. The only valid characters for a domain name are letters, numbers and a hyphen. Also, maximum length for a domain name is 64 characters, which could/should be checked too. > But, even that still doesn't cover all the possible and legal Unicode > code-points that can exist on both sides of the @ of an email address.. No, they can't. There are no 8-bit characters allowed in an email-address. Check out RFC2821. > tedd@ˆ.com > > is a legal and working email address. If that reads "tedd(at)<space>.com", it might be valid on your system, but not in public. /Per Jessen, Zürich |
|
#9
| |||
| |||
| Yeti wrote: > <?php > # this one worked fine for me, but it does not cover the full RFC > like: "name" name@notworking.cc OR name <name@notworking.cc> > $regex = > "^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\ \^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$"; For the domain part, I would check against "@([a-z0-9-]+\.)+[a-z0-9-]+" and then do a lookup for an A record. There are still some patterns that will fit the above, without being valid domain-names. /Per Jessen, Zürich |
|
#10
| |||
| |||
| mike a écrit : > > php should have a good check built-in. > > see http://www.php.net/manual/en/function.filter-var.php Argh ! Howmany times it is in ? I spent so many time to write a regex that belongs the RFC822 :-/ Because all the regex in answer here was false. They don't allow email like "Mickael Doodoo"@lupusmic.com nor mickael+doudou@lupusmic.org ; and they are valuable email addresses. Without the fact that a top level domain isn't always between two and three characters (think about .museum). -- Mickaël Wolff aka Lupus Michaelis http://lupusmic.org |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.