0

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. I used RegexBuddy's simplified RFC 2822 and removed the plus signs from it, to get that expression:

       [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 (i.e. invalidating "aaa+aa@gmail.com"), it fails to correctly invalidate same input when I use the same expression as the ng-pattern

Kepedizer
  • 826
  • 2
  • 10
  • 24
  • 4
    Don't do that; `+` is perfectly valid. – SLaks May 04 '14 at 14:49
  • not saying it isn't.. but if I have to? @SLaks – Kepedizer May 04 '14 at 14:51
  • You want to replace `+` or test if email contains `+` ? – Pedro Lobito May 04 '14 at 15:16
  • [Validating emails with a regex is hard](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address); validating a subset of email addresses with a regex you're trying to write yourself is harder. If you're sole requirement is "should not contain a +" write a solution that does that, and only that. – AD7six May 04 '14 at 15:31

2 Answers2

0

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");
}
Senthil
  • 249
  • 1
  • 2
  • 10
  • I very much doubt the OP actually wants to remove the plus. As stated in the question, the OP wants to validate the address, which your answer is not doing. – dirkk May 04 '14 at 15:18
  • Updated the code to invalidate email with '+' symbol – Senthil May 04 '14 at 15:28
0

Regex to match if email contains + :

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

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

Regex to replace the + from email address:

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

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

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268