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.