简体   繁体   English

使用angularJS和Reg Exp从电子邮件地址中删除“ +”号

[英]removing “+” signs from an email address with angularJS and Reg Exp

I was tasked with making email address with a "+" sign invalid for registry. 我的任务是使带有“ +”号的电子邮件地址对于注册表无效。 I'm new to regex and my research into it led me to believe it's best to state what you want in a valid input and not what you don't want. 我是regex的新手,对它的研究使我相信最好在有效输入中声明您想要的内容,而不是不需要的内容。 I used RegexBuddy's simplified RFC 2822 and removed the plus signs from it, to get that expression: 我使用RegexBuddy的简化RFC 2822并从中删除了加号,以获得该表达式:

       [a-z0-9!#$%&'*/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

while passing RB's test feature (ie invalidating "aaa+aa@gmail.com"), it fails to correctly invalidate same input when I use the same expression as the ng-pattern 通过RB的测试功能(即使“ aaa+aa@gmail.com”无效)时,当我使用与ng-pattern相同的表达式时,它无法正确使相同的输入无效

A quick solution 快速解决方案

To remove + symbol 删除+符号

"aaa+aa@gmail.com".replace(/\+/g,''); // -> aaaaa@gmail.com

To validate 验证

var email = "aaa+aa@gmail.com";
var valid = email.match(/\+/g) ? false : true;
if(valid) {
  console.log("Email Id doesn't contain +");
} else {
  console.log("Email Id contains + Invalid");
}

Regex to match if email contains + : 如果电子邮件包含+ ,则匹配正则表达式:

if (/\+/im.test(email)) {
    alert("INVALID " + email );
} else {
    alert("VALID " + email);
}

http://jsfiddle.net/tuga/6gWvJ/3/ http://jsfiddle.net/tuga/6gWvJ/3/

Regex to replace the + from email address: 正则表达式替换电子邮件地址中的+

email = email.replace(/\+/, "");

http://jsfiddle.net/tuga/7VFNA/2/ http://jsfiddle.net/tuga/7VFNA/2/

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

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