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 Tutorial jQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc
8 min read
Getting Started with jQuery jQuery is a fast lightweight, and feature-rich JavaScript library that simplifies many common tasks in web development. It was created by John Resign and first released in 2006. It makes it easier to manipulate HTML documents, handle events, create animations, and interact with DOM(Document Object M
4 min read
jQuery Introduction jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM). jQuery simplifies HTML document traversing and manipulation, browser event handling, DOM animations, Ajax interactions, and cross-browser Java
7 min read
jQuery Syntax The jQuery syntax is essential for leveraging its full potential in your web projects. It is used to select elements in HTML and perform actions on those elements.jQuery Syntax$(selector).action()Where - $ - It the the shorthand for jQuery function.(selector) - It defines the HTML element that you w
2 min read
jQuery CDN A Content Delivery Network (CDN) is a system of distributed servers that deliver web content to users based on their geographic location. Including the jQuery CDN in your project can improve the performance, enhance reliability, and simplify maintenance.What is a jQuery CDN?A jQuery CDN is a service
4 min read
jQuery Selectors
JQuery SelectorsWhat is a jQuery Selector?jQuery selectors are functions that allow you to target and select HTML elements in the DOM based on element names, IDs, classes, attributes, and more, facilitating manipulation and interaction. Syntax: $(" ")Note: jQuery selector starts with the dollar sign $, and you encl
5 min read
jQuery * SelectorThe jQuery * selector selects all the elements in the document, including HTML, body, and head. If the * selector is used together with another element then it selects all child elements within the element used. Syntax: $("*")Parameters: *: This parameter is used to select all elements.Example 1: Se
1 min read
jQuery #id SelectorjQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. Elaborating on the terms, it simplifies HTML document traversing and manipulation, browser event handling, DOM animations, Aja
1 min read
jQuery .class SelectorThe jQuery .class selector specifies the class for an element to be selected. It should not begin with a number. It gives styling to several HTML elements. Syntax: $(".class") Parameter: class: This parameter is required to specify the class of the elements to be selected.Example 1: In this example,
1 min read
jQuery Events
jQuery Effects
jQuery HTML/CSS
jQuery Traversing