A jQuery Script to Check if a String is a Phone Number or Email Address

Sam Deering
Share

Here’s a simple jQuery code snippet to check if a string is either a phone number or email address. It could be very useful for checking form validation. You can then have a input called “Email or Phone” then on submit check the value and if its a phone number then set value to the phone input and clear email input before form submit.

jQuery Code

//validate name
var name = $('input[name="name"]').val();
if (name.length Phone or email
[js]
//check if they entered a phone number OR email address
var ep_emailval = $('#email').val();
var intRegex = /[0-9 -()+]+$/;
if(intRegex.test(ep_emailval)) {
   console.log("is phone");
   //copy value to phone field
   $('#phone').attr('value',ep_emailval);
   //clear email field
   $('#email').attr('value','');
}

HTML Code

//check if they entered a phone number OR email address