简体   繁体   English

Javascript电子邮件RegEx JSLint有问题吗?

[英]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. 此功能工作正常,但是由于JSLint给我以下错误,因此无法构建。 : 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. 我尝试转义需要转义的字符,这解决了JSLint的问题,但是RegExp不再起作用。

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) 
}

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. 我刚刚在http://www.jslint.com/上进行了检查,它似乎摆脱了该错误。

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

试试这个正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM