0

I am trying to validate email address input by user in javascript something like this

var email = document.getElementById("<%=Email.ClientID%>").value;
                // checks if the e-mail address is valid
                var emailPat = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;
                var matchArray = email.match(emailPat);
                if (matchArray == null) {
                    alert("Not a valid e-mail address");
                    document.getElementById("<%=Email.ClientID%>").focus();

                    return false;


                }

This works great it validates email address ok but it return false on this email address ayaz5+any+thing+you+want@gmail.com what I want is this is also a valid email address what changes do I need to made in this thanks

Jahangeer
  • 94
  • 4
  • 20

1 Answers1

1

Try this code:

var email = 'ayaz5+any+thing+you+want@gmail.com';
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var matchArray = regex.test(email);
if (matchArray) {
    alert("valid");
} else {
    alert("Not a valid e-mail address");
}

DEMO

Manwal
  • 23,450
  • 12
  • 63
  • 93