jQuery Inputmask is a JavaScript library that allows developers to apply masks to input fields. Input masks ensure that user input follows a predefined format, making this plugin especially useful for date inputs, numeric fields, phone numbers, emails, and more. Inputmask provides a rich set of functionalities, making it more versatile than other masking plugins.
The mask plugin is used for the same purpose. But here Inputmask plugin provides some extra features and methods that make it more useful.
Why Use Inputmask?
Inputmask is particularly useful for:
- Ensuring users input data in a specific format, such as dates or phone numbers.
- Reducing errors and ensuring data consistency across input fields.
- Enhancing the user experience by making data entry easier and more intuitive.
This plugin can be applied to various types of input fields, such as:
- Dates: dd/mm/yyyy
- Phone numbers: (999) 999-9999
- Email addresses: {A-Za-z0-9}@gmail.com
- IP addresses: 255.255.255.255
- Numeric fields: 999,999.99
How to Use jQuery Inputmask
Before you can use Inputmask, you must include the plugin in your project. Here's the CDN link for the Inputmask library:
CDN link:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/jquery.inputmask.bundle.min.js"></script>
Note: This link must be included in the index page to make the jQuery input mask functionalities work.
Following are some ways to use Inputmask:
1. Using data-inputmask Attribute
You can define the mask directly in the HTML using the data-inputmask attribute:
<input data-inputmask = " 'mask' : 'xx-xxxxxxx'" />
$(document).ready(function() {
$(":input").inputmask();
}
2. Using Inputmask Plugin on a Target Element
You can directly apply the mask to an element using a jQuery selector. For example:
$("selector").inputmask("xx-xxxxxxxx");
3. Using the Inputmask Class
You can also use the Inputmask class to define and apply masks:
var Mask = new Inputmask("XXXX-XXXX");
Mask.mask("selector");
Types of Input Masking
jQuery Inputmask provides several types of masking for different use cases:
1. Static Masking
This type of masking remains fixed and does not change during input. For instance:
$(document).ready( function() {
$("selector").inputmask("xx-xxxxxxxx");
});
2. JIT Masking (Just-in-Time Masking)
JIT masking applies the mask only as the user types. Use the jitMasking option to enable this:
Inputmask("Password", { jitMasking : true }).mask("SELECTOR");
3. Alternator Masking
When you have multiple formatting options (e.g., date or time), use alternator masking:
$(document).ready( function() {
$("selector").inputmask("XX/XX/XX | XX-XX-XX");
});
4. Optional Masking
You can mark parts of the mask as optional using square brackets [ ]. For example:
Inputmask( '99/99/99[99]' ,).mask("selector");
5. Preprocessing Masking
Preprocessing allows you to generate a custom mask before applying it:
Inputmask( { mask : function() {
return Resulting mask;
}).mask("selector");
6. Dynamic Masking
Dynamic masking lets you define a flexible mask with variable length using {} brackets. For example:
Inputmask( "9{1,5}-9{1,5}").mask("selector");
Common Inputmask Methods
The Inputmask plugin offers a variety of methods to manage masks:
1. mask()
This method is used to apply the mask to a specific element:
Inputmask ( {mask: "XX-XXXX-XXXX" }).mask("selector");
2. remove()
To remove a mask from an input field, use the remove() method:
Inputmask.remove("selector");
3. format()
The format() method allows you to format a given value with a specified mask:
var formattedValue = Inputmask.format( "919867543298",
{ alias:"phonenumber", inputFormat: "(91)-99999-99999"});
4. isComplete()
This method checks if the user has completely filled the input according to the mask:
if (Inputmask.isComplete("input")) {
console.log("Input is complete");
}
5. isValid()
This method verifies whether the input conforms to the mask:
let verify = Inputmask.isValid( "(91)-98675-43289" ,
{ alias : "phonenumber" ,inputFormat: "+91-99999-99999"});
Example: In this example, we will see a demonstration of the input mask.
HTML
<!DOCTYPE HTML>
<html>
<head>
<!-- CDN for the Jquery and inputmask plugin -->
<title>
JQuery input mask phone
number validation
</title>
<link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/jquery.inputmask.bundle.min.js">
</script>
<link rel="stylesheet" href=
"https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
<style>
body,
html {
height: 100%;
background-repeat: no-repeat;
background-color: #d3d3d3;
font-family: 'Oxygen', sans-serif;
}
.main {
margin-top: 70px;
}
h1.title {
font-size: 50px;
font-family: 'Passion One', cursive;
font-weight: 400;
}
hr {
width: 10%;
color: #fff;
}
.form-group {
margin-bottom: 15px;
}
label {
margin-bottom: 15px;
}
input,
input::-webkit-input-placeholder {
font-size: 11px;
padding-top: 3px;
}
.main-login {
background-color: #fff;
border-radius: 2px;
box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
}
.main-center {
margin-top: 30px;
margin: 0 auto;
max-width: 330px;
padding: 40px 40px;
}
i {
margin-right: 5px;
padding-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="row main">
<div class="main-login main-center">
<form class="form-horizontal"
method="post" action="#">
<div class="form-group">
<label for="name"
class="cols-sm-2 control-label">
User Name
</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-user fa"
aria-hidden="true"></i>
</span>
<input type="text" class="form-control"
name="name" id="name"
placeholder="User Name" />
</div>
</div>
</div>
<div class="form-group">
<label for="email"
class="cols-sm-2 control-label">
Date of Birth
</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-birthday-cake fa"
aria-hidden="true"></i>
</span>
<input type="text" class="form-control"
id="dob" placeholder="XX-XX-XXXX" />
</div>
</div>
</div>
<div class="form-group">
<label for="password" class="cols-sm-2 control-label">
Password
</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-lock fa-lg"
aria-hidden="true"></i>
</span>
<input type="password" class="form-control"
name="password" id="password"
placeholder="********" />
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function () {
/* Mask for Geeks Name */
Inputmask("aaaaaaaaaaaaaaaa", {
placeholder: "-",
greedy: false,
casing: "upper",
jitMasking: true
}).mask('#name');
/* Mask for Zip code */
Inputmask("9{2}[-]9{2}[-]9{4}", {
placeholder: "-",
greedy: false
}).mask('#dob');
/* Mask for Password */
Inputmask("*{8,16}", {
placeholder: "-",
greedy: false,
}).mask('#password');
});
</script>
</body>
</html>
Output:
jQuery Inputmask
Similar Reads
jQuery :input Selector
The jQuery :input selector in jQuery is used to select input elements. This selector is also used with button element. Syntax: $(":input")Note: jQuery :input selector not only selects input elements but also Buttons, Dropdowns, and Textarea elements. Example 1: This example use :input selector to co
2 min read
jQuery Examples
The following jQuery section contains a wide collection of jQuery examples. The examples are categorized based on the topics including Selectors, Event methods, Plugins, and many more. Each program example contains multiple approaches to solve the problem. Prerequisite: jQuery TutorialTable of Conte
4 min read
Pure CSS Inputs
Inputs are important in any webpage form. It is used to take various inputs from the user which are essential in collecting data or information. Following are the various types of inputs in Pure CSS: Required InputsDisabled InputsRead Only InputsRounded InputsCheckboxes and Radios Required Inputs: I
3 min read
jQuery :image Selector
The jQuery :image selector is used to select the input elements with a type equal to the image. Syntax: $(":image")Parameter: :enabled selector: It is used for selecting HTML input elements for changing its properties, e.g. background-color, etc. Example 1: HTML <!DOCTYPE html> <html>
1 min read
Blaze UI Inputs
Blaze UI is a free open source UI Toolkit that provides an excellent structure for building websites quickly with a scalable and maintainable foundation. All components in Blaze UI are developed mobile-first and rely solely on native browser features, not a separate library or framework. It helps us
3 min read
Semantic-UI Input Types
Semantic UI is an open-source front-end development framework similar to Bootstrap to design an attractive web page. It has lots of components that help to create an attractive and responsive website layout. Semantic-UI Input is a field that is used to get input from the users. Input type define the
1 min read
Pure CSS Rounded Inputs
Pure CSS is a framework of CSS. It is a free and open-source tool collection for creating responsive websites and web applications. Pure CSS is developed by Yahoo and is used for creating faster, beautiful, and responsive websites. It can be used as an alternative to Bootstrap. Pure CSS provides a "
1 min read
jQuery focus() Method
The jQuery focus() method is used to set the focus on a specified element, such as an input field or textarea. It triggers the browser's focus event, enabling user interaction with the element, like typing or clicking.Syntax:$(selector).focus(function)Here selector is the selected element. Parameter
2 min read
Semantic-UI Input States
Semantic UI is an open-source framework that offers us special classes to create stunning and responsive user interfaces. It makes use of jQuery and CSS to create pleasant user interfaces that are very much like bootstrap. It has many classes which are used for styling different elements in the HTML
3 min read
Pure.CSS Required Inputs
In some cases when we ask someone to fill a form and few things are optional and few things are a must to fill in that form. In that case, we can use the HTML required in Pure CSS as well. This attribute works with other types of input like radio, checkbox, number, text, etc. Syntax: <input requi
1 min read