Home > Software design >  Allow single quote in Email Regx validation
Allow single quote in Email Regx validation

Time:07-22

I want to allow a single quote in email while doing the javascript email validation.

I have used the following code but it's working as expected.

 var pattern = new RegExp(/^([a-zA-Z0-9_\-\.] )@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-] \.) ))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
 return pattern.test(emailAddress);

Following emails it should return as valid

 zoe.o’[email protected] 
 natalie.o'[email protected]

CodePudding user response:

Just need to add ' and to the first character class

Changing: [a-zA-Z0-9_\-\.] to [a-zA-Z0-9_\-\.'’]

Fixed:

var pattern = new RegExp(/^([a-zA-Z0-9_\-\.'’] )@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-] \.) ))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
return pattern.test(emailAddress);

Do be aware of Keith's comment. There is more info in a question here.

  • Related