angularregexangular-formly

Angular email pattern regex validation with apostrophe i.e. '


Thank you in advance for your help.

I am trying to validate the email address entered with regex pattern i.e. "pattern": [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$ which is working as expected but I would like this pattern also check for apostrophe i.e. '

I have seen so many examples suggested by devs in stackoverflow but nothing helped. Could you please help me to resolve it.

Example I tried so far is :

"pattern": [a-zA-Z0-9._%+-']+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$

"pattern": [a-zA-Z0-9._%+-\']+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$

"pattern": ['][a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$

I am getting error as its inside double quotes saying invalid character


Solution

  • let pattern = /^(?<user>[a-zA-Z0-9._%+'-]+)@(?<domain>[a-zA-Z0-9.'-]+\.[a-zA-Z]{2,4})$/;
    

    Apostrophe added, unnecessary escapings removed.

    See https://regex101.com/r/QUonyq/1.

    Although, your regular expression is not the one that validates email addresses. See https://stackoverflow.com/a/201378/6632736 for the real one.