Email validation in Javascript for a specific scenario

57 views Asked by At

I have tried approximately all the regex solution but I am still able to bypass the validation for the below email -

[email protected]

I need to mark this email as invalid using Javascript. Please share the correct regex that I can use to mark above email as invalid.

2

There are 2 answers

0
Wenli He On BEST ANSWER

You can use this one from Regex101

I think your previous regex might accidentally include \. inside character list with + quantifier, so @xx..co still can be bypass

var regex = /^[0-9a-zA-Z-_\$#]+@[0-9a-zA-Z-_\$#]+\.[a-zA-Z]{2,5}/;
console.log(regex.test('[email protected]'));

0
Dave On

Try this:

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\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,}))$/;
    return re.test(email);
}

console.log(validateEmail("[email protected]"));
console.log(validateEmail("[email protected]"));