iSofia
iSofia

Reputation: 1522

REGEX to match domain in email addresses

I'm currently using this very relaxed REGEX to validate email addresses:

(/(.+)@(.+)\.(.+){2,}/.test(emailAddress)
  1. I believe that it only allows min1_allowsDots@min1_allowsDots.min2 - is this correct?

  2. How should I modify it to match only a particular domain - anything@ onlythisdomain.com?

TIA!

iSofia

Upvotes: 0

Views: 967

Answers (3)

Dangelo Santana
Dangelo Santana

Reputation: 31

Maybe this could help you:

^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?
(company_domain1|company_domain2)\.ca$/g

It is a regex where you can validate the email including a specific domain such as [email protected] or [email protected] and it will match only if the email is ending with @doctorgroup.ca or @doctors.ca.

Upvotes: 0

Ian Hazzard
Ian Hazzard

Reputation: 7771

This simple RegEx basically says:

At least one character, then an "@", then literally "gmail.com".

If you change the domain to something else, it will not match.

var email = '[email protected]';

var re = /.+\@gmail\.com/;

alert(email.match(re));

Upvotes: 0

vks
vks

Reputation: 67968

(.+)@onlythisdomain\.(.+){2,}

This should do it.If .com is also fixed use

(.+)@onlythisdomain\.com

Upvotes: 2

Related Questions