How to Validate Phone Number using jQuery Input Mask Plugin ?
Last Updated :
01 Jun, 2023
An input mask is a string expression that demonstrates the format of a valid user input value or we can say that it constrains user input. This is very useful if there are certain inputs in forms that require validation. In this article, we will learn how to apply an input mask for validating a phone number in an input element using jQuery.
There are two approaches that can be taken here:
Approach 1: Using the jQuery inputmask plugin, there are three input fields defined in the following example, each having a class of first-phone, second-phone and third-phone respectively. We select these elements using the jQuery class selector [$(".class-name")] and then apply the inputmask() method from the inputmask plugin on each input element. The parameter of this inputmask() method takes the specified string expression to constraint the user input to. In this case, we set three different formats of phone numbers for the three input fields.
CDN Link:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/5.0.7/jquery.inputmask.min.js"></script>
Note: This link must be included in the index page to utilise the jQuery inputmask plugin and thus implement all its functionalities.
Syntax:
$(document).ready(function(){
// A static mask
$(selector).inputmask("99-9999999");
// Mask which specifies options
$(selector).inputmask({"mask": "(999)-999-9999"});
});
Example: Below example illustrates the use of jQuery Input Mask Phone Number Validation using .inputmask() plugin.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<!-- Required for using jQuery input mask plugin -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/5.0.7/jquery.inputmask.min.js">
</script>
<!-- Basic inline styling -->
<style>
body {
text-align: center;
}
h1 {
color: green;
font-size: 40px;
}
p {
font-size: 30px;
font-weight: bold;
}
div:not(:last-child) {
margin-bottom: 2rem;
}
</style>
</head>
<body>
<h1>GeeksForGeeks</h1>
<p>jQuery - Input Mask Phone Number Validation</p>
<form>
<div>
<label>Phone Number 1:</label>
<input type="text"
name="phone"
class="first-phone"
placeholder="(999)-999-9999" />
</div>
<div>
<label>Phone Number 2:</label>
<input type="text"
name="phone"
class="second-phone"
placeholder="(99)-9999-9999" />
</div>
<div>
<label>Phone Number 3:</label>
<input type="text"
name="phone"
class="third-phone"
placeholder="+99-9999999999" />
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$(".first-phone").inputmask("(999)-999-9999");
$(".second-phone").inputmask("(99)-9999-9999");
$(".third-phone").inputmask("+99-9999999999");
});
</script>
</body>
</html>
Output:

Approach 2: Using the jQuery maskedinput plugin. There are four input fields defined in the following example, each having a class of first-phone, second-phone, third-phone and fourth-phone respectively. We select these elements using the jQuery class selector [$(".class-name")] and then apply the mask() method from the maskedinput plugin on each input element. The parameter of this mask() method takes the specified string expression to constraint the user input to. In this case, we set four different formats of phone numbers for the four input fields.
CDN Link:
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js">
</script>
Note: This link must be included in the index page to utilise the jQuery maskedinput plugin and thus implement all its functionalities.
Syntax:
$(document).ready(function(){
$(selector).mask("99-9999999");
});
Example: Below is the example that illustrates the use of JQuery Input Mask Phone Number Validation using .mask() plugin.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<!-- Required for using jQuery maskedinput plugin -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js">
</script>
<!-- Basic inline styling -->
<style>
body {
text-align: center;
}
h1 {
color: green;
font-size: 40px;
}
p {
font-size: 30px;
font-weight: bold;
}
div:not(:last-child) {
margin-bottom: 2rem;
}
</style>
</head>
<body>
<h1>GeeksForGeeks</h1>
<p>jQuery - Input Mask Phone Number Validation</p>
<form>
<div>
<label>Phone Number 1:</label>
<input type="text"
name="phone"
class="first-phone"
placeholder="(99)-9999-9999" />
</div>
<div>
<label>Phone Number 2:</label>
<input type="text"
name="phone"
class="second-phone"
placeholder="+99-9999999999" />
</div>
<div>
<label>Phone Number 3:</label>
<input type="text"
name="phone"
class="third-phone"
placeholder="(999)-999-9999" />
</div>
<div>
<label>Phone Number 4:</label>
<input type="text"
name="phone"
class="fourth-phone"
placeholder="+999-99-99-999999" />
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$(".first-phone").mask("(99)-9999-9999");
$(".second-phone").mask("+99-9999999999");
$(".third-phone").mask("(999)-999-9999");
$(".fourth-phone").mask("+999-99-99-999999");
});
</script>
</body>
</html>
Output:

Similar Reads
How to make a Value/Text Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Value/Text Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ h
1 min read
How to make a Password Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Password Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hre
1 min read
How to make an URL Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating an URL Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ href=âh
1 min read
How to create a Telephone Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Telephone Input Area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstyleshee
1 min read
How to create a Number Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops In this article, we will be creating a number input area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ h
1 min read
How to make an Email Input using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets,an and desktops. In this article, we will be creating an Email Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hr
1 min read
How to Validate Number String in JavaScript ?
Validating a number string in JavaScript involves ensuring that a given string represents a valid number. This typically includes checking for digits, optional signs (+/-), decimal points, and possibly exponent notation (e.g., "1.23e4"). We will use various methods to validate number strings in Java
2 min read
How to create a Placeholder Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Placeholder Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ
1 min read
Properly Store and Validate Phone Numbers in Django Models
Choosing the best way to store phone numbers in Django models depends on your application's requirements and complexity. Using Django CharField with validation is suitable for basic needs, while third-party libraries django-phonenumber-field provide advanced features for handling international phone
3 min read
How to create a Month Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Month Input area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ h
1 min read