• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Input Mask Phone Number Using jQuery

Input Mask Phone Number Using jQuery

December 5, 2021 Leave a Comment

To validate a phone number using jQuery, the simplest way is to use the jQuery inputmask() function.

$("#phone-number-input").inputmask('(999)-999-9999')

To use the jQuery inputmask() function, you will need to include an additional script on your page.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/5.0.6/jquery.inputmask.min.js"></script>

Let’s say we have the following form. In the form, we have a phone number field for our user to fill out.

<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
<label for="phonenumber">Phone Number:</label>
<input type="text" id="pnumber" name="phonenumber">
</form>

<button type="submit" form="form1" value="Submit">Submit</button>

We can mask the phone number field with the following jQuery code:

$("#pnumber").inputmask('(999)-999-9999')

If you are using WordPress, don’t forget to change the $ to jQuery:

jQuery("#pnumber").inputmask('(999)-999-9999')

The final code and output for this example of how to mask a phone number using the jQuery and Javascript is below:

Code Output:






Full Code:

<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
<label for="phonenumber">Phone Number:</label>
<input type="text" id="pnumber" name="phonenumber">
</form>

<button type="submit" form="form1" value="Submit">Submit</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/5.0.6/jquery.inputmask.min.js"></script>

<script>

$(document).ready(function() {
    $("#pnumber").inputmask('(999)-999-9999');
}); 

</script>

Phone Number Mask for International Numbers in JQuery

The example above was for using a phone number inside the United States. If you are working with users who have international phone numbers, we can easily validate an international phone number using jQuery with the same inputmask() method.

Let’s say we have the same form as above.

<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
<label for="phonenumber">Phone Number:</label>
<input type="text" id="pnumber" name="phonenumber">
</form>

<button type="submit" form="form1" value="Submit">Submit</button>

In this case, we just need to change what is inside the call to the inputmask() method.

$("#pnumber").inputmask('+99-9999999999');

The final code and output for this example of how to mask an international phone number using the jQuery and Javascript is below:

Code Output:






Full Code:

<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
<label for="phonenumber">Phone Number:</label>
<input type="text" id="pnumber" name="phonenumber">

<button type="submit" form="form1" value="Submit">Submit</button>
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/5.0.6/jquery.inputmask.min.js"></script>

<script>

$(document).ready(function() {
    $("#pnumber").inputmask('+99-9999999999');
}); 

</script>

Hopefully this article has been helpful for you to be able to validate both US phone numbers and international phone numbers using the jQuery inputmask() function.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Value of a Radio Button
  • 2.  jQuery siblings – Get the Sibling Elements of a Div
  • 3.  Using jQuery to Add id to div
  • 4.  Get nth Child with jQuery
  • 5.  Get Selected Option from Dropdown Using jQuery
  • 6.  Using jQuery to Check if Element Has Class
  • 7.  Using jQuery to Add a Sibling to an Element
  • 8.  Using jQuery to Remove the Id of a Div
  • 9.  jQuery fadeOut – Fading Out Element in HTML
  • 10.  jQuery Display None

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy