I need help on finding a regex expression that will match email addresses of only a specific domain
As in any .*@testdomain.com
And also the opposite any thing other than .*@testdomain.com
In python: You haven't tell us what kind of regex flavor you need however this example will fit most of them: Assuming Unix style where . is any character: .*\..*@gmail\.com I used follwing regex expression to validate the email address.
Email Hunter is a very easy online service that lets you find out Email address of a particular domain. Go to the website, just enter the domain name of your desired company like ampercent.com to find email addresses available in that domain.
Matching an email address within a string is a hard task, because the specification defining it, the RFC2822 , is complex making it hard to implement as a regex. For more details why it is not a good idea to match an email with a regex, please refer to the antipattern example when not to use a regex: for matching emails .
When you need to rapidly validate an entry to make sure it looks like an email, the best option is to keep it simple: That regex will check that the mail address is a non-space separated sequence of characters of length greater than one, followed by an @, followed by two sequences of non-spaces characters of length two or more separated by a . .
Ay
I propose an expression very simple:
^[A-Za-z0-9._%+-][email protected]$
and for the negative check:
^[A-Za-z0-9._%+-]+@(?!testdomain.com)[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$
I hope could work for you
@Kushal I found an easy way to do this, but it was somewhat tricky, because I had to retrieve via ajax the domain from a global variable stored somewhere else.
Here is the following line of code inside a javascript validation method:
return new RegExp("^\\w+([-+.']\w+)*@"+getDomain.responseJSON.d+"$").test(value.trim());
I create a new RegExp on the spot and test the value (email input) to see if it matches the domain.
If you want something more elaborate but have standard domains, then it can go like this:
return /^\w+([-+.']\w+)*@?(example1.com|example2.com)$/.test(value.trim());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With