1

I am working on email validation for a asp.net server control.

Below are the sample emails system should allow -

test@domain.com
test@domain.subdomain.com
v-test@domain.com
v-test@domain.subdomain.com

Can anyone please tell me how to write a regular expression for the above email validation.

Here is my code working correctly for simple emails like test@domain.com etc

var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/

var EmailmatchArray = UserEmail.match(emailPat);

if (EmailmatchArray == null) {
document.getElementById('<%=lblEmailErrorCtrl.ClientID%>').innerText = "Invalid email address";
return false;

}
  • A quick [Google search](https://www.google.com/search?q=email+validation+regex&oq=email+validation+regex&aqs=chrome..69i57.263j0j7&sourceid=chrome&es_sm=93&ie=UTF-8) for "email validation regex" shows some things that might come in handy, namely [this question](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address). – Nic Apr 15 '15 at 02:46

1 Answers1

0

Note that this will not magically validate all valid emails - that task is nearly impossible. To fix your situation though, try:

var emailPat = /^(\".\"|[-A-Za-z]\w)@([\d{1,3}(.\d{1,3}){3}]|[A-Za-z]\w*(.[A-Za-z]\w*)+)$/
Syntax
  • 111
  • 6