Sanath Sanath - 2 months ago 11
Javascript Question

regular expression validating email with specific domain/subdomain

I need to validate email with specific domain name and with/without a subdomain, using javascript.

abcdefgh@abc.com - valid
abcdefgh@subdomain.abc.com - valid
abcdefgh@abc.com.au - invalid
abcdefgh@subdomain.abc.com.au - invalid
abcdefgh@subdomain.subdomain.abc.com - invalid
abcdefgh@subdomain.subdomain.abc.com.au - invalid


The reg exp that I used.

^[a-zA-Z]{1,}+@[a-zA-Z]{1,}\.abc\.com$

Answer

Make the subdomain part as optional.

^[a-zA-Z]+@(?:[a-zA-Z]+\.abc|\.abc)\.com$

or

^[a-zA-Z]+@(?:[a-zA-Z]+\.)?abc\.com$
Comments