Zerotoinfinity
Zerotoinfinity

Reputation: 6540

Regular expression for email to allow only two domain

I have to create a regular expression for email id like this

[email protected] and [email protected]

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 ASP.NET 2.0

Upvotes: 1

Views: 9560

Answers (4)

strager
strager

Reputation: 90062

Replace:

yahoo\.com

with:

(yahoo\.com|gmail\.com)

or:

((yahoo|gmail)\.com)  

Upvotes: 6

Chris
Chris

Reputation: 27627

To put an alternative in you just need to do (yahoo\.com|gmail\.com) and that should match either one.

Upvotes: 2

darioo
darioo

Reputation: 47213

  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

Upvotes: 0

alpha-mouse
alpha-mouse

Reputation: 5003

+([-+.]\w+)*@(?:yahoo|gmail).com

Upvotes: 1

Related Questions