Skip to main content

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

By Sam Deering



Free JavaScript Book!

Write powerful, clean and maintainable JavaScript.

RRP $11.95

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 < 3)
    alert('Please enter a name 3 characters or more.');
    return false;

//validate email
var email = $('input[name="email"]').val(),
    emailReg = /^([w-.]+@([w-]+.)+[w-]{2,4})?$/;
if(!emailReg.test(email) || email == '')
     alert('Please enter a valid email address.');
     return false;

//validate phone
var phone = $('input[name="phone"]').val(),
    intRegex = /[0-9 -()+]+$/;
if((phone.length < 6) || (!intRegex.test(phone)))
     alert('Please enter a valid phone number.');
     return false;

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',''); }


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

Related Articles

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

Learn the basics of programming with the web's most popular language - JavaScript

A practical guide to leading radical innovation and growth.

Integromat Tower Ad