Reputation: 3120
I'm trying to validate that an email address is in the correct format by using the below function.
This function works fine however it will not build as JSLint gives me the following error.
: error (Lint occured): Unescaped '['.
I have tried escaping the characters which need to be escaped, this fixed the problem with JSLint but the RegExp no longer worked.
Any help here on what I should do?
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 email.match(re)
}
Upvotes: 0
Views: 1372
Reputation: 11056
If you escape the opening '[
' (as well as the closing ']
') in your character classes that should fix things:
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,}))$/;
I've just checked this on http://www.jslint.com/ and it seemed to get rid of the error.
Upvotes: 1