views:

316

answers:

4

Duplicate of lots of other questions:

http://stackoverflow.com/search?q=validate+email

Hi, I want to validate emailid by java script. I am using following code.

var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/ ;
var emailid=document.getElementById("<%=txtEmail.ClientID %>").value; 
var matchArray = emailid.match(emailPat);
if (txtemail.value!="")
{  
  if (matchArray == null)
  {
        alert("Your email address seems to be incorrect. \n Please type the proper email address and try again.") 
        return false
  }
}

This code is working for emails like, [email protected]

But when i enter valid mailid like [email protected] or [email protected] , it is showing alert. What changes should I do in my coding..?? so it will not show alert for valid mail ids like above..?? Can anyone give me suggestions..?? Thanks in advance..

+2  A: 

Nice info with many examples:

http://www.regular-expressions.info/email.html

btw: + is valid character, I hate validators where I can't enter [email protected] (because gmail supports this)

DiGi
+1  A: 

I'd try changing your regular expression to this:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])
SquidScareMe
+2  A: 

There are quite a few questions about regexs for email validation.

Sam Hasler
+1  A: 

http://regexlib.com/Search.aspx?k=&amp;c=1&amp;m=3&amp;ps=50

Use whichever suits you best.

Cerebrus