Reputation: 1853
I am still figuring my way around regex and have come across a problem that I am trying to solve. How do I validate for multiple specific email addresses?
For example, I want to only allow testdomain.com, realdomain.com, gooddomain.com to be validated. All other email addresses are not allowed.
[email protected] OK
[email protected] OK
[email protected] OK
[email protected] NOT OK
But I'm stil unclear on how to add multiple specific email addresses for the regex.
Any and all help would be appreciated.
Thank you,
Upvotes: 0
Views: 195
Reputation: 43703
The official standard is known as RFC 2822.
Use OR operator |
for all domain names you want to allow. Do not forget to escape .
in the domain.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:testdomain\.com|realdomain\.com|gooddomain\.com)
Also use case-insensitivity modifier/flag to allow capital letters in the address.
Upvotes: 0
Reputation: 5578
To make this expandable to many domains, I would probably capture the domain name and then compare that captured domain name with your whitelist in code.
.+@(.+)
First, ".+" will match any number (more than 0) of any characters up until the last "@" symobol in the string. Second, "@" will match the "@" symbol. Third, "(.+)" will match and capture (capture because of the parenthesis) any character string after the "@" symbol.
Then, depending on the language you are using, you can get the captured string. Then you can see if that captured string is in your domain whitelist. Note, you'll want to do a case insensitive comparison in this last step.
Upvotes: 0
Reputation:
Assuming the above works for testdomain:
\b[A-Z0-9._%-]+@(?:testdomain|realdomain|gooddomain)\.com\b
Also, please note that you will have to add a case insensitive i
modifier for this to work with your test cases, or use [A-Za-z0-9._%-]
instead of [A-Z0-9._%-]
See here
Upvotes: 0
Reputation: 6561
You didn't specify which language you're using, but most regex implementations have a notion of logical operators, so the domain part of your pattern would have something like:
(domain1|domain2|domain3)
Upvotes: 2
Reputation: 2639
\b[A-Z0-9._%-]+@(testdomain|realdomain|gooddomain)\.com\b
Upvotes: 1
Reputation: 58619
Do you mean to include various ligitimate domains in one regex?
\b[A-Z0-9._%-]+@(testdomain|gooddomain|realdomain)\.com\b
Upvotes: 3