Validate email address in Javascript?

0 votes
162 views
asked Jun 21, 2013 in HTML by john (4,220 points)
hi all

Validate email address in Javascript?

1 Answer

0 votes
answered Jun 21, 2013 by kalpana (3,995 points)

Using Regular Expressions is probably the best way. Here's an example (live demo):

function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

...