Email validation with regex
This question already has an answer here:
With your currently setted regular expression your are only aceppting one char at the beginning and one char at the end. A valid email for your regex would be for example a@test.a
But you dont want that so change it to: var regex = /^w+@w+.w{2,3}$/;
You missed the plus char which means that you can enter at least one or more chars. The top level domain usually consists of two or three chars so you can limit it.
Now it should work.
Unless you know for sure that email addresses entered will match that format, you should consider using a regular expression that matches more.
"()<>[]:,;@"!#$%&'*+-/=?^_`{}| ~.a"@üñîçøðé.com is technically(!) a correct email address. While it may not be a good idea to match all of this craziness, this may be what you are looking for and will match the vast majority of email addresses today:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
Again, if you are sure your email addresses will be in the format you were going for originally, go with Valinho's answer.
An interesting read about matching email addressses with regular expressions (contains the above regex): http://www.regular-expressions.info/email.html
And the official standard (without unicode madness): https://tools.ietf.org/html/rfc5322
链接地址: http://www.djcxy.com/p/16508.html上一篇: JavaScript电子邮件验证不起作用
下一篇: 用正则表达式进行邮件验证