Validating a Phone Number in HTML and JavaScript 

Data collection from customers is an ongoing need for commercial enterprises. In most cases, two primary obstacles will be overcome throughout this procedure. 

The first step is to get the data, and the second is to check its accuracy. Collecting and verifying some data, such as ages and names, can be less difficult than collecting and verifying other data, such as email addresses and phone numbers.

Checking the correctness and reliability of the information acquired from customers for use in a commercial setting is what’s meant by “data validation.” 

HTML and JavaScript are among the most powerful tools for determining whether or not a phone number is legitimate. This article explains HTML and JavaScript and the instructions for using them to verify a phone number.

HTML and JavaScript: A Brief Introduction About the Two Giants

Before we get into the grand scheme of things, first and foremost, let’s start with the basic definition of HTML and JavaScript. 

  • What is HTML?

Web users can build and organize sections, paragraphs, and links using HTML elements, tags, and attributes. 

HTML is also known as Hypertext Markup Language. On the other hand, HTML is not regarded as a programming language since it cannot provide dynamic functionality. This fact is important to keep in mind.

To be specific, HTML has a wide variety of applications:

  • Internet Navigation – Because HTML often includes hyperlinks, users can move between similar pages and websites and add connections to those pages and websites.
  • Web Development – HTML code is used by developers to create how a browser displays the components of a web page, including text, hyperlinks, and media files. Also, looking to improve the web development process, follow these tips!
  • Web Documentation – Documents can be organized and formatted using HTML comparable to that of Microsoft Word. 
  • Now, what is JavaScript?

JavaScript is a scripting language to integrate advanced website features, such as real-time information, interactive maps, and animated 2D/3D visuals. Network-centric applications can be built using this simple and fast-interpreted programming language.

A typical web technology cake has three layers. HTML and CSS are the first two layers, with JavaScript being the third. The Hypertext Markup Language (HTML) is a markup language for structuring and interpreting online information.

HTML enables website visitors to build sections, define paragraphs, employ components, tags, etc., arrange links, and embed pictures and videos. HTML is valuable for web construction, online navigation, and web page documentation.

When it comes to design, though, CSS stands for Cascading style sheets. To define the display of documents produced in a markup language like HTML, it uses a style sheet language called SVG. Styles like colors, fonts, and more can be applied to HTML text using CSS.

JavaScript is harder to learn than HTML and CSS. Using JavaScript, on the other hand, has various benefits, including.

  • It’s a great way to give your website or web page some more oomph.
  • The use of JavaScript enhances a website’s interactivity and usability.
  • Using JavaScript, you can reduce the amount of data sent to the server by verifying user input.
  • It’s simple to use and offers a wide range of options on the website.
  • JavaScript validates user data and input, enhancing data security.

How to Validate a Phone Number Using JavaScript Regex?

JavaScript Regex refers to “JavaScript Regular expressions.” A search pattern can be considered a string of characters arranged in a certain order. You’ll be able to verify phone numbers in Javascript if you use it.

The following is the JavaScript Regular Expression that you will be using to validate the JS Phone Number Validation:

/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/

What exactly does it signify? Let’s analyze it in more detail.

  • /^\(? – It’s possible that the number will begin with an open parenthesis.
  • (\d{3}) – After that, you’ll need to input three numbers to ensure the format is legitimate. If there aren’t any brackets around the number, the numeral must begin with these digits.
  • \)? – You will be able to incorporate a closing parenthesis thanks to it.
  • [- ]? – A hyphen can be included in the string, although its presence is not required. Depending on your preference, it might go either after the parentheses or after the first three numbers. Take, for instance, the (123)- or 123- prefix.
  • (\d{3}) – If this is the case, the number must include three digits. For instance, it can look like (123), 456, or 123456. 
  • [- ]? – It gives you the choice of adding a hyphen at the end of the number, such as (123)-456-, -123-, or 123456-.
  • (\d{4})$/ – In the end, the sequence has to conclude with digit 4. Take, for instance, the number (123)-456-7890, which can also be written as 123-456-7890 or 123456-7890.

How do you utilize the Regex pattern matching tool in JavaScript? The following is a method that can be utilized by you in order to validate a phone number:

function validatePhoneNumber(input_str) {

  var re = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;

  return re.test(input_str);


You can verify phone numbers that have been submitted using this format by making use of this function:

  • (123) 456-7890
  • (123)456-7890
  • 123-456-7890
  • 1234567890

How to Validate a Phone Number Using a JavaScript Regex and HTML?

Simply adhering to these procedures will allow you to validate a phone number using the JavaScript regular expression and HTML:

Step 1: Make a file in the HTML format. After that, put these lines here:

div class=”p-4″

  form id=”myform”


    label for=”myform_phone” Phone: /label

  input type=”text” id=”myform_phone” name=”phone” 

    div id=”phone_error” class=”error hidden” Please enter a valid phone number /div


  div class=”mt-2″

    button type=”submit” Submit





Step 2: Generate a CSS file. After that, put this piece of code:



  color: red;

  size: 80%






Step 3: Start a JavaScript file. Insert these lines here:

function validatePhoneNumber(input_str) {

  var re = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;

  return re.test(input_str);


function validateForm(event) {

  var phone = document.getElementById(‘myform_phone’).value;

  if (!validatePhoneNumber(phone)) {


  } else {


    alert(“validation success”)




document.getElementById(‘myform’).addEventListener(‘submit’, validateForm);

Step 4: The final product will appear in this format:

Validate phone number. A web page with inputs.

If you add a phone number using the appropriate format, such as (123) 456-7890, you will get a notification stating that the validation was successful.

If you want to take the help of an IT service for this critical job, learn about the pros and cons of outsourcing one before beginning!

Regional Phone Number Formats

Using an incorrect phone number format can lead to broken integrations, inaccurate data, and the creation of several entries. It might make it quite tough to utilize your phone number data manually. 

That’s why phone numbers in the right format are critical. Take a look at the most typical regional telecommunications systems:

  • International Dialing Codes 

For non-NANP nations, the country and area codes must be added to international phone numbers to format them. Immediately before the country code, you must add a “+” symbol. 

In addition, you must use spaces to separate groups of digits in the international phone number. Here’s a case in point: The phone number is 415 555 0132. 

Area codes are another prevalent problem. Most nations’ mobile phone numbers don’t utilize area codes, unlike the US and Canada. The national dialing format is used instead.

  • Cell Phone Numbers 

US phone numbers are 11-digits long, with the 1-digit country code followed by a 3-digit area code and a 7-digit number. The following is an illustration of how this works.

Validate phone number. Phone number example.

  • UK Format

Phone numbers in the United Kingdom are preceded by the country code and a plus (+) symbol. Add the area code and a comma after that. 

Following this pattern is the only way to dial a phone number in London. Remember that London’s area code is 20. There is no way to call an Oxford phone number without dialing 01.

Validate phone number. Numbers written in red.

  • North American Format 

Numbers in the United States and Canada are formatted differently. A parenthetical enclosing of the area code is required. Afterward, it would help if you left a gap. 

Once you’ve entered the three-digit exchange code and the four-digit number, you’ll need to hyphenate them. There’s an example of this here.

Validate phone number. Phone number example

Main Reason to Validate Phone Number Input 

You should check that people’s phone numbers are correct to reduce the risk of sign-up spam and fraud and to spot simple mistakes like typos. 

In most cases, you will also want to ensure that the user has access to the phone number they are providing; therefore, we will include some recommendations for phone verification and some additional best practices for account security at the very end. 

This is because you will typically want to ensure that the user has access to the phone number they are providing. Just like this, your business should also invest in cloud computing. Why? Find here!

Validate phone number. A photo of a submit form.

The Takeaway!

Validating the data provided by users guarantees you will get the accurate information necessary for your company. 

Ultimately, we wanted to discuss the topic of validating a phone number via JavaScript. Your company will be protected from fraudulent activity if you verify the digits of your phone number.