Using Regex to validate E-mail and Phone Number


Read {count} times since 2020

Here is the RegEx expression to validate Phone number and E-mail.

RegEx expression for validating E-mail :

/^(([^<>()[].,;: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,}))$/

and the RegEx expression for validating Phone number :

/^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/

Here is an example of usage of these RegEx expressions in JavaScript.

var emailregex=/^(([^<>()[].,;: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,}))$/;var phoneregex=/^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;if (emailregex.test(string)){// valid email}else{// Invalid email}if (phoneregex.test(string)){// valid phone number}else{// Invalid phone number}

You can replace string with whatever you want. For Example:

if (phoneregex.test($("#phone").val())){// valid phone number}else{// Invalid phone number}

Enjoy !!.

Show Comments