I have to create a regular expression for email id like this
xyz@yahoo.com and xyz@gmail.com
I need to allow only yahoo and gmail as the domain not any other domain. I have used this expression \w+([-+.]\w+)*@yahoo.com . It is working fine for yahoo. but I want to include gmail also. How can I modify it to except gmail also?
I am using AS开发者_Python百科P.NET 2.0
Replace:
yahoo\.com
with:
(yahoo\.com|gmail\.com)
or:
((yahoo|gmail)\.com)
To put an alternative in you just need to do (yahoo\.com|gmail\.com)
and that should match either one.
+([-+.]\w+)*@(?:yahoo|gmail).com
- read on this article on email validation since your regex isn't perfect, and if false positives/negatives are a concern
- first check if your email string contains "@yahoo" or "@gmail"
- use information from 1. to check if that string is a valid email
Update: a ready made email validator can be found here; since it's from MSDN, it should be correct
精彩评论