JavaScript Regex to validate a phone number

Posted by I. B. Gd Pramana A. Putra, 24 Aug 22, last updated 24 Aug 22

Do you need a quick example of JavaScript Regex to validate a phone number? We have the code example for you that you can follow easily:

function is_number_valid(val) {
    const re = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/;
    return re.test(val);

console.log("01234567890: " + is_number_valid("01234567890")); // true
console.log("123-456-7890: " + is_number_valid("123-456-7890")); // true
console.log("123.456.7890: " + is_number_valid("123.456.7890")); // true
console.log("123 456 7890: " + is_number_valid("123 456 7890")); // true
console.log("(123) 456 7890: " + is_number_valid("(123) 456 7890")); // true
console.log("1: " + is_number_valid("1")); // false

Copy and paste this code snippet as you're building a validation feature to validate phone numbers.

I love sharing code snippets as most of the time, a quick code example is what we're looking for instead of long-written articles. If you think my code snippets are helpful and save you a lot of time, please consider buying me a cup of coffee :)

Support me via · paypal · buymeacoffee · ko-fi · trakteer
Contributed Snippets
Answer & Responses
    No comments yet

Wanna write a response?

You have to login before write a comment to this post.