shane87
shane87

Reputation: 3120

Javascript Email RegEx Problem with JSLint?

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

Answers (2)

Ian Oxley
Ian Oxley

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

Headshota
Headshota

Reputation: 21439

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

Try this regex

Upvotes: 0

Related Questions