0

I have a regex for my email:

const EMAIL = /^(([^<>()\[\].,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

I want to validate below email as true:

Fred\ Bloggs@example.com true
Joe.\\Blow@example.com true
""test\\blah""@example.com true

Below email as false: ""test\blah""@example.com

currently, I'm not able to do this validation please help

ArpitG
  • 11
  • 3
  • The HTML(5) spec has a relatively simple regex for emails that native form validation uses (https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address): `/^[a-zA-Z0-9.!#$%&'*+\/=?^_\`{|}~-]+@[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])?)*$/`. Probably it makes sense to reuse it for consistency? – Ilya Streltsyn Sep 19 '18 at 15:14
  • Possible duplicate of [How to validate an email address in JavaScript?](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – ponury-kostek Sep 19 '18 at 15:39
  • thanks for your reply https://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/ want validation for this article – ArpitG Sep 19 '18 at 15:41
  • https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript – NAVIN Sep 19 '18 at 17:12

2 Answers2

0

function validateEmail(email) {
    var re = /(([^<>()\[\].,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
    return re.test(String(email).toLowerCase());
}
var test_email1 ='""test\blah""@example.com"';
alert(validateEmail(test_email1));
var test_email2 ='Fred\ Bloggs@example.com';
alert(validateEmail(test_email2));
var test_email3 ='Joe.\\Blow@example.com';
alert(validateEmail(test_email3));
var test_email4 ='""test\\blah""@example.com';
alert(validateEmail(test_email4));
Victoria Le
  • 860
  • 6
  • 15
  • thinks it's passing most of test cases except ""test\blah""@example.com should give false and ""test\\blah""@example.com as true – ArpitG Sep 19 '18 at 17:11
  • this is"not\allowed@example.com it's passing this test case but should not pass as it has only one " . – ArpitG Sep 19 '18 at 17:31
  • looks like you changed the test case and remove the double quotes at the end of the false example. Just to be clear, it would be true if there is space after the single \ but it would be false if there is a \ and follow with a char or num? – Victoria Le Sep 19 '18 at 18:44
  • my bad i changed it after i was i posted wrong test case – ArpitG Sep 20 '18 at 16:44
0

Here is the function from Omid's answer using a regex that meets RFC 2822.

function validateEmail(email) {
    var re = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
    return re.test(String(email).toLowerCase());
}

console.log(validateEmail('Fred\ Bloggs@example.com')); // logs true
console.log(validateEmail('Joe.\\Blow@example.com')); // logs true
console.log(validateEmail('""test\\blah""@example.com')); // logs true

You'll notice this function doesn't return false on the case you'd like to fail.

The only way to really test an email address is to send an email to it -- don't rely on regex to verify email addresses because your regex will never be perfect.

Community
  • 1
  • 1
jacobherrington
  • 447
  • 4
  • 17