Top Related Projects
Input Mask plugin
A jQuery Plugin to make masks on form fields and HTML elements.
Format input text content when you are typing...
Input mask for React, Angular, Ember, Vue, & plain JavaScript
A powerful, feature-rich, random test data generator.
Quick Overview
jQuery Masked Input is a jQuery plugin that allows developers to easily create masked input fields for web forms. It provides a simple way to enforce specific input formats for things like phone numbers, dates, and social security numbers, enhancing user experience and data consistency.
Pros
- Easy to implement and integrate with existing jQuery projects
- Supports a wide range of input mask patterns
- Customizable placeholder characters and settings
- Lightweight and performant
Cons
- Requires jQuery as a dependency
- Limited built-in mask definitions (though custom masks can be created)
- Not actively maintained (last update was in 2017)
- May not be the best choice for modern JavaScript frameworks
Code Examples
- Basic phone number mask:
$('#phone').mask('(999) 999-9999');
- Date mask with custom placeholder:
$('#date').mask('99/99/9999', {placeholder: 'mm/dd/yyyy'});
- Custom mask for a product code:
$.mask.definitions['~'] = '[A-Za-z]';
$('#product-code').mask('~~~~~-99999');
- Callback function on complete:
$('#ssn').mask('999-99-9999', {
onComplete: function(cep) {
console.log('Mask is complete', cep);
}
});
Getting Started
- Include jQuery and the Masked Input plugin in your HTML:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>
- Apply the mask to your input fields:
$(document).ready(function() {
$('#phone').mask('(999) 999-9999');
$('#date').mask('99/99/9999');
$('#ssn').mask('999-99-9999');
});
- Customize as needed using the available options and definitions.
Competitor Comparisons
Input Mask plugin
Pros of Inputmask
- More extensive feature set, including support for various input types and formats
- Regular updates and active maintenance
- Dependency-free, can be used without jQuery
Cons of Inputmask
- Steeper learning curve due to more complex configuration options
- Larger file size, which may impact page load times
Code Comparison
Inputmask:
Inputmask("99/99/9999").mask(document.getElementById("date"));
jquery.maskedinput:
$("#date").mask("99/99/9999");
Both libraries offer similar basic functionality for masking input fields. However, Inputmask provides more advanced features and customization options, while jquery.maskedinput focuses on simplicity and ease of use.
Inputmask supports a wider range of input types and formats, making it more versatile for complex input masking requirements. It also doesn't depend on jQuery, which can be beneficial for projects not using this library.
On the other hand, jquery.maskedinput has a simpler API and may be easier to implement for basic masking needs. Its smaller file size could be advantageous for projects prioritizing performance.
Ultimately, the choice between these libraries depends on the specific requirements of your project, the desired level of customization, and whether jQuery dependency is a concern.
A jQuery Plugin to make masks on form fields and HTML elements.
Pros of jQuery-Mask-Plugin
- More actively maintained with recent updates
- Supports a wider range of input types and formats
- Offers more customization options and flexibility
Cons of jQuery-Mask-Plugin
- Slightly larger file size
- May have a steeper learning curve for complex masks
Code Comparison
jquery.maskedinput:
$("#date").mask("99/99/9999");
$("#phone").mask("(999) 999-9999");
$("#ssn").mask("999-99-9999");
jQuery-Mask-Plugin:
$('#date').mask('00/00/0000');
$('#phone').mask('(000) 000-0000');
$('#ssn').mask('000-00-0000');
$('#custom').mask('AA-00-SSSS', {
translation: { 'A': {pattern: /[A-Z]/}, 'S': {pattern: /[a-zA-Z]/} }
});
Both plugins offer similar basic functionality for common input masks. However, jQuery-Mask-Plugin provides more advanced features, such as custom translations and patterns, allowing for more complex and flexible mask definitions. This makes it suitable for a wider range of use cases, especially when dealing with non-standard input formats or international variations.
Format input text content when you are typing...
Pros of Cleave.js
- No jQuery dependency, making it more lightweight and versatile
- Supports more input types, including credit cards, numerals, and dates
- Offers real-time formatting as the user types
Cons of Cleave.js
- Less extensive documentation compared to jQuery.maskedinput
- Fewer customization options for complex masks
- Slightly steeper learning curve for beginners
Code Comparison
Cleave.js:
var cleave = new Cleave('.input-element', {
numeral: true,
numeralThousandsGroupStyle: 'thousand'
});
jQuery.maskedinput:
jQuery(function($){
$("#date").mask("99/99/9999");
$("#phone").mask("(999) 999-9999");
});
Both libraries aim to provide input masking functionality, but they differ in their approach and implementation. Cleave.js offers a more modern, flexible solution without jQuery dependency, while jQuery.maskedinput provides a simpler, jQuery-based approach with a focus on traditional input masking.
Cleave.js is better suited for projects that don't use jQuery or require more diverse input formatting options. jQuery.maskedinput might be preferable for jQuery-based projects or those needing simple, predefined mask patterns.
Ultimately, the choice between these libraries depends on project requirements, existing dependencies, and the specific input formatting needs of the application.
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Pros of text-mask
- Framework-agnostic, supports React, Angular, Vue, and vanilla JavaScript
- More flexible and customizable input masking options
- Active development and maintenance
Cons of text-mask
- Larger bundle size due to its modular architecture
- Steeper learning curve for advanced customizations
Code Comparison
text-mask:
import createNumberMask from 'text-mask-addons/dist/createNumberMask'
const numberMask = createNumberMask({
prefix: '$',
allowDecimal: true,
includeThousandsSeparator: true,
})
jquery.maskedinput:
jQuery(function($){
$("#date").mask("99/99/9999");
$("#phone").mask("(999) 999-9999");
$("#tin").mask("99-9999999");
});
Key Differences
- text-mask offers more granular control over mask creation
- jquery.maskedinput relies on jQuery, while text-mask is framework-independent
- text-mask provides a more modern approach to input masking
Use Cases
- text-mask: Ideal for complex, customized input masks in modern web applications
- jquery.maskedinput: Better suited for simpler masking needs in jQuery-based projects
Community and Support
- text-mask has a larger and more active community
- jquery.maskedinput has been around longer but has less frequent updates
Performance
- text-mask may have slightly higher overhead due to its flexibility
- jquery.maskedinput is lightweight but limited to jQuery environments
A powerful, feature-rich, random test data generator.
Pros of generatedata
- More comprehensive data generation capabilities, supporting various data types and formats
- Offers a web-based interface for easy data generation without coding
- Actively maintained with regular updates and improvements
Cons of generatedata
- Larger and more complex project, potentially harder to integrate into existing workflows
- Requires more setup and configuration compared to the simpler jquery.maskedinput
Code Comparison
generatedata example (PHP):
$generator = new \GDGenerator();
$generator->addDataType('names', ['numRows' => 100]);
$generator->addDataType('email', ['numRows' => 100]);
$data = $generator->generate();
jquery.maskedinput example (JavaScript):
$("#date").mask("99/99/9999");
$("#phone").mask("(999) 999-9999");
$("#tin").mask("99-9999999");
Summary
generatedata is a more powerful and versatile tool for generating various types of data, while jquery.maskedinput focuses specifically on input masking for form fields. generatedata offers a wider range of features and a user-friendly interface, but may be overkill for simple input formatting tasks. jquery.maskedinput is lightweight and easy to implement for basic input masking needs, but lacks the extensive data generation capabilities of generatedata.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Masked Input Plugin for jQuery
Notice: This project is no longer being maintained.
I started this project over 10 years ago to fill a need for a side project I was working on at the time. Nothing ever became of that side project, but this little plugin lived on. Over the years it brought me joy to stumble on sites using this thing. It was super encouraging to hear from people using it in their own products. I tried for a while to maintain it, even after I had moved away from front end web development.
The time has come to officially call it quits. The web has changed(A LOT) and there are better things out there like Cleave.js. I'll leave this repo up for posterity in an archived state. Thank you to everyone who contributed to or used this plugin over the years.
Overview
This is a masked input plugin for the jQuery javascript library. It allows a user to more easily enter fixed width input where you would like them to enter the data in a certain format (dates,phone numbers, etc). It has been tested on Internet Explorer, Firefox, Safari, Opera, and Chrome. A mask is defined by a format made up of mask literals and mask definitions. Any character not in the definitions list below is considered a mask literal. Mask literals will be automatically entered for the user as they type and will not be able to be removed by the user.The following mask definitions are predefined:
- a - Represents an alpha character (A-Z,a-z)
- 9 - Represents a numeric character (0-9)
- * - Represents an alphanumeric character (A-Z,a-z,0-9)
Usage
First, include the jQuery and masked input javascript files.
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>
Next, call the mask function for those items you wish to have masked.
jQuery(function($){
$("#date").mask("99/99/9999");
$("#phone").mask("(999) 999-9999");
$("#tin").mask("99-9999999");
$("#ssn").mask("999-99-9999");
});
Optionally, if you are not satisfied with the underscore ('_') character as a placeholder, you may pass an optional argument to the maskedinput method.
jQuery(function($){
$("#product").mask("99/99/9999",{placeholder:" "});
});
Optionally, if you would like to execute a function once the mask has been completed, you can specify that function as an optional argument to the maskedinput method.
jQuery(function($){
$("#product").mask("99/99/9999",{completed:function(){alert("You typed the following: "+this.val());}});
});
Optionally, if you would like to disable the automatic discarding of the uncomplete input, you may pass an optional argument to the maskedinput method
jQuery(function($){
$("#product").mask("99/99/9999",{autoclear: false});
});
You can now supply your own mask definitions.
jQuery(function($){
$.mask.definitions['~']='[+-]';
$("#eyescript").mask("~9.99 ~9.99 999");
});
You can have part of your mask be optional. Anything listed after '?' within the mask is considered optional user input. The common example for this is phone number + optional extension.
jQuery(function($){
$("#phone").mask("(999) 999-9999? x99999");
});
If your requirements aren't met by the predefined placeholders, you can always add your own. For example, maybe you need a mask to only allow hexadecimal characters. You can add your own definition for a placeholder, say 'h', like so: $.mask.definitions['h'] = "[A-Fa-f0-9]";
Then you can use that to mask for something like css colors in hex with a mask "#hhhhhh"
.
jQuery(function($){
$("#phone").mask("#hhhhhh");
});
By design, this plugin will reject input which doesn't complete the mask. You can bypass this by using a '?' character at the position where you would like to consider input optional. For example, a mask of "(999) 999-9999? x99999" would require only the first 10 digits of a phone number with extension being optional.
Getting the bits
We generally recommend that you use bower to install jquery.maskedinput plugin.
$ bower install --save jquery.maskedinput
Setting up your Developer Environment
jQuery Masked Input uses NodeJS and GruntJS as it's developer platform and build automation tool.
To get your environment setup correctly, you'll need nodejs version 0.8.25 or greater installed. You'll also need to install the grunt command line tool:
$ sudo npm install -g grunt-cli
Once node is installed on your system all that you need to do is install the developer dependencies and run the grunt build:
$ npm install
$ grunt
All of the tests for jQuery Masked Input are run using the jasmine test runner.
Top Related Projects
Input Mask plugin
A jQuery Plugin to make masks on form fields and HTML elements.
Format input text content when you are typing...
Input mask for React, Angular, Ember, Vue, & plain JavaScript
A powerful, feature-rich, random test data generator.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot