Search code examples
javascriptemail-validation

Email validation in Javascript for a specific scenario


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.


Solution

  • 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]'));