Question:
How do I validate an email addres with regular expressions?
Jason
2006-02-07 04:57:15 UTC
Hi, I'm using VB.NET and I have a regular expression validator to check if the entered email address is valid. I have the following regular expression:
[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+

It works fine, but when I try to add more e-mail addresses (separated by commas and possibly spaces after the commas) it doesn't work, obviously. I have no idea about regular expressions, can anyone please tell me what to add to my existing expression? Thanks
Three answers:
Dookie
2006-02-08 01:17:45 UTC
Kheme, your answer is as intelligent as you are. How about reading the questions first?

Jason, here goes:



^ ( ( [ a - z A - Z 0 - 9 _ \ - \ . ] + ) @ ( [ a - z A - Z 0 - 9 _ \ - \ . ] + ) \ . ( [ a - z A - Z ] { 2 , 5 } ) { 1 , 25 } ) + ( [ , . ] [ \ s ] * ( ( [ a - z A - Z 0 - 9 _ \ - \ . ] + ) @ ( [ a - z A - Z 0 - 9 _ \ - \ . ] + ) \ . ( [ a - z A - Z ] { 2 , 5 } ) { 1 , 25 } ) + ) * $



It checks for an email address, then for a "," (replace with ; or anything else if you wish), then for one or more spaces, and then for another email address.

Match examples:



email@email.com

email@email.com,email1@mail.ca

email@email.com, email1@email1.com

email@email.com, email1@email1.com, email2@email.com

email@email.com, -infinite numer of spaces- email1@email1.com



It won't find a match if you leave a space BEFORE the commas.

Hope that helped!
wd5gnr
2006-02-07 05:13:25 UTC
So why not (with spaces added for Yahoo):



[\w\, ]*((.+ )@(.*)\....?)



This would take two or three character domains (com, edu, us, etc.)



Depending on your parser, it may need some tweaking to work with things like al@research.ibm.com
Kheme
2006-02-07 06:27:16 UTC
Separate them with semicolons, not commas.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...