正则表达式,不允许在电子邮件中的任何地方使用空格 [英] Regular expression to not allow spaces anywhere in the email
问题描述
我正在尝试使用正则表达式验证电子邮件地址,以便如果在电子邮件中的任何位置添加空格都会引发错误.我正在使用的当前正则表达式是这样:
I am trying to validate the email address using a regular expression such that it throws an error if white spaces are added anywhere in the email. The current regex I am using is this:
<input type="email" class="form-control" name="username" ng- model="username" id="email" placeholder="Email" ng-pattern='/^(([^<>()\ [\]\\.,;:\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,}))$/' required />
<span ng-show="login.username.$error.pattern">This email format is invalid! </span>
问题在于它允许我最后添加空格.如何修改它,以便在最后添加空白时会触发错误消息?
The problem is it allows me to add white spaces in the end. How can I modify it so that if I add a whitespace in the end the error message is fired?
推荐答案
angularJs的最新版本(1.5.7)包括对电子邮件地址验证的一些改进.
The newest release of angularJs (1.5.7) includes some email address validation improvements.
您可以在此处检查提交.
第28行对应于已提交的正则表达式:
Line 28 corresponds to the committed regex:
var EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;
在指令的代码下方,您具有进行测试以验证正则表达式的测试.它确实检查空格.
Below the directive's code you have the tests to validate the regex. It does check for white spaces.
这篇关于正则表达式,不允许在电子邮件中的任何地方使用空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!