Top Related Projects
Input Mask plugin
Format input text content when you are typing...
jQuery Masked Input Plugin
Input mask for React, Angular, Ember, Vue, & plain JavaScript
A powerful, feature-rich, random test data generator.
VanillaMasker is a pure javascript mask input
Quick Overview
jQuery Mask Plugin is a lightweight and customizable input masking library for jQuery. It allows developers to easily add input masks to form fields, ensuring that users enter data in a specific format, such as phone numbers, dates, or currency values.
Pros
- Easy to use and integrate with existing jQuery projects
- Supports a wide range of mask types and custom mask definitions
- Lightweight and performant, with minimal impact on page load times
- Actively maintained with regular updates and bug fixes
Cons
- Requires jQuery as a dependency, which may not be ideal for modern projects
- Limited support for complex masking scenarios or advanced validation
- May not be suitable for projects using newer JavaScript frameworks or vanilla JS
Code Examples
- Basic phone number mask:
$('#phone').mask('(000) 000-0000');
This code applies a mask to an input field with the ID "phone", formatting the input as a US phone number.
- Date mask with placeholder:
$('#date').mask('00/00/0000', {placeholder: "__/__/____"});
This example adds a date mask to an input field, including a placeholder to guide user input.
- Currency mask with custom options:
$('#price').mask('#,##0.00', {
reverse: true,
translation: {
'#': {pattern: /[0-9]/, optional: true}
}
});
This code creates a currency mask with custom options, including reverse input and custom translation for the '#' character.
Getting Started
To use jQuery Mask Plugin in your project, follow these steps:
- Include jQuery and the jQuery Mask 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.mask/1.14.16/jquery.mask.min.js"></script>
- Apply masks to your input fields using JavaScript:
$(document).ready(function() {
$('#phone').mask('(000) 000-0000');
$('#date').mask('00/00/0000');
$('#zipcode').mask('00000-000');
});
This code applies masks to phone number, date, and zip code input fields when the document is ready.
Competitor Comparisons
Input Mask plugin
Pros of Inputmask
- More flexible and feature-rich, supporting a wider range of input types and formats
- Better performance, especially for complex masks and large datasets
- Active development and maintenance, with regular updates and bug fixes
Cons of Inputmask
- Steeper learning curve due to more complex configuration options
- Larger file size, which may impact page load times for smaller projects
- May be overkill for simple masking needs
Code Comparison
jQuery-Mask-Plugin:
$('#date').mask('00/00/0000');
$('#phone').mask('(000) 000-0000');
$('#money').mask('000.000.000.000.000,00', {reverse: true});
Inputmask:
Inputmask("99/99/9999").mask("#date");
Inputmask("(999) 999-9999").mask("#phone");
Inputmask("currency", {
prefix: "$ ",
groupSeparator: ",",
alias: "numeric",
digits: 2,
digitsOptional: false
}).mask("#money");
Both libraries offer similar basic functionality for common masking needs. However, Inputmask provides more advanced options and customization, as seen in the currency mask example. jQuery-Mask-Plugin offers a simpler syntax for basic use cases, making it easier to implement for straightforward masking requirements.
Format input text content when you are typing...
Pros of Cleave.js
- Lightweight and dependency-free, unlike jQuery-Mask-Plugin which requires jQuery
- Supports more input types, including credit cards, dates, and numerals
- Offers more customization options and formatting flexibility
Cons of Cleave.js
- Slightly steeper learning curve due to more configuration options
- May require more setup code for simple masking tasks
- Less widespread adoption compared to jQuery-Mask-Plugin
Code Comparison
jQuery-Mask-Plugin:
$('#phone').mask('(000) 000-0000');
Cleave.js:
new Cleave('#phone', {
phone: true,
phoneRegionCode: 'US'
});
Both libraries aim to simplify input formatting, but Cleave.js offers more granular control at the cost of slightly more verbose initialization. jQuery-Mask-Plugin provides a simpler API for basic masking needs, while Cleave.js excels in handling complex formatting scenarios and various input types.
Cleave.js is better suited for modern web applications that don't rely on jQuery, offering a more flexible and feature-rich solution. However, for projects already using jQuery or requiring simple masking, jQuery-Mask-Plugin remains a solid choice due to its ease of use and widespread adoption.
jQuery Masked Input Plugin
Pros of jquery.maskedinput
- More established and mature project with a longer history
- Supports a wider range of browsers, including older versions
- Provides more customization options for complex input patterns
Cons of jquery.maskedinput
- Less actively maintained, with fewer recent updates
- Larger file size, which may impact page load times
- More complex syntax for defining masks
Code Comparison
jQuery-Mask-Plugin:
$('#date').mask('00/00/0000');
$('#phone').mask('(000) 000-0000');
$('#ssn').mask('000-00-0000');
jquery.maskedinput:
$("#date").mask("99/99/9999");
$("#phone").mask("(999) 999-9999");
$("#ssn").mask("999-99-9999");
Both plugins offer similar functionality for basic input masking. jQuery-Mask-Plugin uses '0' for digits, while jquery.maskedinput uses '9'. The syntax is slightly different, but the end result is comparable for simple masks.
jQuery-Mask-Plugin generally offers a more straightforward and intuitive API for basic use cases, while jquery.maskedinput provides more advanced features for complex scenarios. The choice between the two depends on specific project requirements, browser support needs, and desired customization options.
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Pros of text-mask
- Framework-agnostic, works with vanilla JavaScript, React, Angular, and more
- Smaller bundle size and better performance due to not relying on jQuery
- More flexible and customizable masking options
Cons of text-mask
- Less mature and potentially less stable compared to jQuery-Mask-Plugin
- Smaller community and fewer resources available for support
- May require more setup and configuration for complex use cases
Code Comparison
jQuery-Mask-Plugin:
$('#phone').mask('(000) 000-0000');
text-mask:
import createNumberMask from 'text-mask-addons/dist/createNumberMask'
const phoneMask = ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
Summary
text-mask offers a more modern, flexible approach to input masking without jQuery dependency. It supports various frameworks and provides more customization options. However, jQuery-Mask-Plugin has a larger community and may be easier to implement for simple use cases. The choice between the two depends on project requirements, preferred framework, and desired level of customization.
A powerful, feature-rich, random test data generator.
Pros of generatedata
- More comprehensive data generation capabilities, allowing for creation of various data types and formats
- Offers a web-based interface for easy data generation without coding
- Supports multiple output formats, including SQL, CSV, XML, and JSON
Cons of generatedata
- Larger and more complex project, potentially harder to integrate into existing applications
- Requires more setup and configuration compared to a simple jQuery plugin
- May have a steeper learning curve for users unfamiliar with data generation concepts
Code Comparison
generatedata (PHP):
$generator = new \GDGenerator($this->dataTypes, $this->exportTypes, $this->countries);
$generator->generate($numRows, $data);
$output = $generator->generateExportData($exportType, $data);
jQuery-Mask-Plugin (JavaScript):
$('.date').mask('00/00/0000');
$('.time').mask('00:00:00');
$('.phone_with_ddd').mask('(00) 0000-0000');
While generatedata offers more extensive data generation capabilities, jQuery-Mask-Plugin provides a simpler solution for input masking in web forms. generatedata is better suited for creating large datasets or mock data, while jQuery-Mask-Plugin excels at formatting user input in real-time.
VanillaMasker is a pure javascript mask input
Pros of Vanilla-masker
- Lightweight and dependency-free, no need for jQuery
- Potentially better performance due to native JavaScript implementation
- More flexible, can be used with any JavaScript framework or vanilla JS
Cons of Vanilla-masker
- Less extensive documentation and examples compared to jQuery-Mask-Plugin
- Smaller community and fewer contributors, potentially slower development
- May require more manual setup and configuration
Code Comparison
jQuery-Mask-Plugin:
$('#date').mask('00/00/0000');
$('#phone').mask('(000) 000-0000');
$('#money').mask('000.000.000.000.000,00', {reverse: true});
Vanilla-masker:
VMasker(document.querySelector('#date')).maskPattern('99/99/9999');
VMasker(document.querySelector('#phone')).maskPattern('(999) 999-9999');
VMasker(document.querySelector('#money')).maskMoney();
Both libraries offer similar functionality for masking input fields, but Vanilla-masker uses native JavaScript methods while jQuery-Mask-Plugin relies on jQuery. Vanilla-masker's syntax is slightly more verbose but doesn't require jQuery as a dependency. The choice between the two may depend on project requirements, existing dependencies, and personal preference for vanilla JS or jQuery-based solutions.
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
jQuery Mask Plugin
A jQuery Plugin to make masks on form fields and HTML elements.
Documentation, Demos & Usage Examples
https://igorescobar.github.io/jQuery-Mask-Plugin/
Features
- Lightweight (~2kb minified, ~1kb gziped).
- Built-in support for dynamically added elements.
- Masks on any HTML element (no need to server-side mask anymore!)!
- HTML notation support (data-mask, data-mask-recursive, data-mask-clearifnotmatch).
- String/Numeric/Alpha/Mixed masks.
- Reverse mask support for masks on numeric fields.
- Sanitization.
- Optional digits.
- Recursive Digits.
- Fallback Digits.
- Advanced mask initialization.
- Advanced Callbacks.
- On-the-fly mask change.
- Mask removal.
- Full customization.
- Compatibility with React/UMD/Zepto.js/Angular.JS.
- HTML5 placeholder support.
- Clear the field if it not matches support.
Want to buy me a beer? :heart_eyes:
Install it via Package Managers
Bower
bower install jquery-mask-plugin
NPM
npm i jquery-mask-plugin
Meteor
meteor add igorescobar:jquery-mask-plugin
Packagist/Composer
composer require igorescobar/jquery-mask-plugin
CDNs
CDNjs
https://cdnjs.com/libraries/jquery.mask
JSDelivr
http://www.jsdelivr.com/projects/jquery.mask
RubyGems
gem 'jquery_mask_rails' # more details at http://bit.ly/jquery-mask-gem
Tutorials
English
Portuguese
Fun (or not) facts
Compatibility
jQuery Mask Plugin has been tested with jQuery 1.7+ on all major browsers:
- Firefox 2+ (Win, Mac, Linux);
- IE7+ (Win);
- Chrome 6+ (Win, Mac, Linux, Android, iPhone);
- Safari 3.2+ (Win, Mac, iPhone);
- Opera 8+ (Win, Mac, Linux, Android, iPhone).
- Android Default Browser v4+
Typescript support
Definition can be found here.
To install, open terminal and navigate to your working directory.
Typescript 1.x users
- Install typings by running
npm install typings --global
. - Then install the definition by running
typings install dt~jquery-mask-plugin --global --save
.
Typescript 2.x users
- Use npm
npm install --save-dev @types/jquery-mask-plugin
.
For configuration options and troubleshooting refer to these repositories:
Problems or Questions?
Before opening a new issue take a look on those frequently asked questions:
How to integrate with React.js?
How to integrate with Angular.js?
How to integrate with Vue.js?
Problems with old versions of Android keyboard
Negative numbers, or currency related problems
Prefix or sufix on the Mask
Add validation?
Field type number, email not working?
Want to keep the placeholder as the user types?
E-mail mask?
Bugs?
Did you read our docs? Yes? Cool! So now... make sure that you have a functional jsfiddle exemplifying your problem and open an issue for us. Don't know how to do it? Use this fiddle example.
Contributing
- Bug Reporting: Yes! You can contribute opening issues!
- Documenting: Do you think that something in our docs should be better? Do you have a cool idea to increase the awesomeness? Summit your pull request with your idea!
- Bug Fixing: No time to lose? Fix it and help others! Write some tests to make sure that everything are working propertly.
- Improving: Open an issue and lets discuss it. Just to make sure that you're on the right track.
- Sharing: Yes! Have we saved some of your time? Are you enjoying our mask plugin? Sharing is caring! Tweet it! Facebook it! Linkedin It(?!) :D
- Donating: Hey, now that you don't need to worry about masks again... buy me a coffee, beer or a PlayStation 4 (Xbox One also accepted!) :o)
Unit Tests
We use QUnit and GruntJS. To run our test suit is just run: grunt test
in your console or you can open those test-for*.html
files inside of our test/
folder.
In case you're familiar with Docker here is how you can use it:
docker build -t jquery-mask .
CONTAINER_ID=$(docker run -d -v $PWD:/app/jquery-mask-plugin jquery-mask)
docker exec $CONTAINER_ID sh -c "npm install"
docker exec -it $CONTAINER_ID /bin/bash
grunt test
Contributors
Top Related Projects
Input Mask plugin
Format input text content when you are typing...
jQuery Masked Input Plugin
Input mask for React, Angular, Ember, Vue, & plain JavaScript
A powerful, feature-rich, random test data generator.
VanillaMasker is a pure javascript mask input
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