Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to validate two different domain email addresses using Regular Expression.
we have two domains e.g mydomain.com and newdomain.com.au

For single domain it is working as:

[RegularExpression(@"^[a-zA-Z0-9._%+-]+(@mydomain\.com)$", ErrorMessage = "Registration limited to mydomain")]

How can I add both of the domains, can anyone assist?
Posted
Updated 6-May-15 18:27pm
v3

You don't need regular expression. This validation is already done in the class System.Uri, by its constructor. You can find an example here: https://msdn.microsoft.com/en-us/library/system.uri.urischememailto%28v=vs.110%29.aspx[^].

Also, you should handle and catch exceptions, as the string might not be valid. For the exceptions which can be thrown, please see: https://msdn.microsoft.com/en-us/library/z6c2z492%28v=vs.110%29.aspx[^].

If you don't expect "mailto:" in the test string, you have to prefix your input string with it. You can take it from System.Uri.UriSchemeMailto.

Put it all together to create the validation. As an additional bonus, you will get not just true/false answer, but, in case of successful validation, the instance of Systen.Uri, which is much more useful/informative than raw string.

—SA
 
Share this answer
 
Your Regex should be like


RegularExpression(@"^[a-zA-Z0-9._%+-]+((@mydomain\.com)|(@newdomain\.com.\au))$"
 
Share this answer
 
Comments
Mostafa Asaduzzaman 7-May-15 0:43am    
fantastic,
Appreciate your response @Suvabrata
Suvabrata Roy 7-May-15 1:00am    
Welcome...
[no name] 7-May-15 0:50am    
Good Answer +5
Suvabrata Roy 7-May-15 1:01am    
Thanks..
To validate email using regular expression

function validate(your_email)
{
var regilar_exp= /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

return regilar_exp.test(email);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900