Lightweight Input Masking for Web Devs – Masky.js
Masky.js lets you create various input masks. You can easily add prefixes and suffixes to your masks, and set up dynamic formatting to handle different input needs. Need to validate Brazilian CPF or CNPJ numbers? Masky.js has you covered with native validation support for these document types.
In addition, the library dynamically sets the inputmode attribute, optimizing the keyboard layout for mobile users based on the mask. It also calculates and applies minlength and maxlength attributes based on your mask, reducing boilerplate and improving form validation.
1. Download Masky.js and include the minified script in your HTML:
<script src="masky.min.js"></script>
2. To apply a mask, use the data-mask attribute on your input fields. Masky.js uses tokens to define the mask pattern:
For example, to create a mask for a US phone number like (555) 555-1234, use the following:
<input type="text" data-mask="(000) 000-0000" />
3. Use data-mask-prefix and data-mask-suffix to include additional characters before or after the input:
<input type="text" data-mask="000-0000" data-mask-prefix="+01 " data-mask-suffix=" After" />
4. Masky.js supports built-in validation for CPF and CNPJ. Use the data-mask-validation attribute:
// CPF validation: <input type="text" data-mask="000.000.000-00" data-mask-validation="cpf" />
// CNPJ validation: <input type="text" data-mask="00.000.000/0000-00" data-mask-validation="cnpj" />
v1.0.2 (01/27/2026)
The post Lightweight Input Masking for Web Devs – Masky.js appeared first on CSS Script.
NORTHAMPTON — Light rain and cool temperatures didn’t dampen the spirits at Hampshire Pride, which…
SOUTHAMPTON — Residents took the first step to passing either a $2.5 or $1.9 million Proposition…
SUNDERLAND — In a 430-188 vote, Sunderland voters gave the final approval needed for an…
WILLIAMSBURG — Voters will decide one contested race in Monday’s town election ballot as incumbent…
HADLEY — Up to $1.5 million in municipal spending that depends on the successful passage…
BOSTON — “Quabbin – Yesterday and Today” is an exhibit documenting the Quabbin Reservoir through…
This website uses cookies.