开发者

Regular expression for email to allow only two domain

开发者 https://www.devze.com 2023-01-27 14:20 出处:网络
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+

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


  1. read on this article on email validation since your regex isn't perfect, and if false positives/negatives are a concern
  2. first check if your email string contains "@yahoo" or "@gmail"
  3. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消