Input masks that know Indian formats
masklet formats what users type, while they type. It checks the Aadhaar checksum, the GSTIN check character and card numbers, and groups rupees in lakh and crore. Plain JavaScript and React 19.
Live demo
attachMask(input, 'aadhaar')- Formatted
- empty
- Raw value
- empty
- Complete
- No
- Checksum valid
- Not yet
The fake numbers are made up. They pass the Verhoeff checksum but belong to no one. Do not type a real Aadhaar number.
Small, careful masks for real forms
A mask helps users type the right thing. masklet also tells you when the value is complete and when it passes the format's own checks.
Indian formats built in
Aadhaar, PAN, GSTIN, IFSC, PIN code, Indian mobile numbers and rupee amounts. Pass a name like 'pan'.
Real checks
The Aadhaar Verhoeff checksum, the GSTIN check character, the card Luhn check, real dates with leap years, and expiry dates in the future.
The caret stays put
Type in the middle, delete over a space, replace a selection or paste. The blinking cursor stays where users expect it.
Pasting just works
Paste +91 98765 43210, 919876543210 or 09876543210. The field shows +91 98765 43210.
Phone keyboards
Sets inputmode, so phones show a number pad for digits, and autocomplete where a standard hint exists.
Plain JS and React 19
attachMask() for any input. MaskedInput and useMask for React 18 and 19, with no findDOMNode.
Every preset, live
Each field below uses attachMask(input, name). Type into it, or press "Fill example" for a made-up value. The readouts show what your code gets.
Indian IDs
Phone
Date and time
Cards
All examples are made up. The Aadhaar and GSTIN examples were created to pass the checksums. The card numbers are public test numbers. A valid checksum only means the number is well formed. It does not mean the number was issued.
Lakh and crore, the Indian way
In India, large numbers are grouped in lakh and crore. A lakh is 1,00,000 (one hundred thousand). A crore is 1,00,00,000 (ten million). The inr mask groups amounts this way as the user types.
Type an amount
'inr'Type an amount
The words line is worked out by this page, not by masklet. masklet gives the raw value "" as a plain number string.
Grouping examples
format(value, mask)| Typed | Field shows |
|---|---|
| 999 | 999 |
| 1000 | 1,000 |
| 100000 | 1,00,000 |
| 12345678.50 | 1,23,45,678.50 |
Shown with createMask('inr', { symbol: false }). By default the field starts with ₹. Use formatINR(12345678.5) to show an amount in a table: ₹1,23,45,678.50.
Make your own pattern
Type a pattern. The test field below uses it right away. Each token is a place where the user types one character. Every other character is fixed: the mask adds it by itself.
Pattern and test field
attachMask()- Formatted
- empty
- Raw value
- empty
- Complete
- No
Your code
attachMask(input, 'AA 00 AA 0000')Tokens
DEFAULT_TOKENS| Token | Accepts |
|---|---|
0 or 9 | A digit |
a | A letter |
A | A letter, made upper case |
* | A letter or digit |
X | A letter or digit, letters made upper case |
\ | Makes the next character fixed, even if it is a token. In +\91, the 9 is fixed. |
| Anything else | Fixed. The mask adds it by itself, like -, /, ( or a space. |
In JavaScript strings, write a backslash twice: '+\\91 00000-00000'. The code box on the left does this for you. A preset name always wins over a pattern with the same text.
The cursor stays where you expect
The caret is the blinking cursor in the field. Many masks jump it to the end after each change. masklet keeps it in place when you type in the middle, delete over a space, or paste.
Try it
'aadhaar'Caret position: none
- Type in the middle. Click between two digits and type a digit. The digits after it move right, and the caret stays after what you typed.
- Backspace over a space. Put the caret just after a space and press Backspace. The digit before the space is deleted, not the space.
- Paste. Copy one of the values on the right and paste it. Both give the same result.
Paste these
format(text, 'aadhaar')| Pasted text | Field shows | Actions |
|---|---|---|
| 234567890124 | 2345 6789 0124 | |
| 2345 6789 0124 | 2345 6789 0124 | |
| 2345-6789-0124 | 2345 6789 0124 |
The "Paste" buttons put the text into the field the same way a paste does, through the field's input event. 2345 6789 0124 is the made-up example from the README.
A component and a hook
Import from masklet/react. It works with React 18 and 19, server rendering and Next.js. The code below is shown, not run.
MaskedInput takes every normal <input> prop. onValueChange gets the raw value, the formatted value and complete.
import { useState } from 'react'
import { MaskedInput } from 'masklet/react'
export function AadhaarField() {
const [aadhaar, setAadhaar] = useState('')
return (
<label>
Aadhaar number
<MaskedInput
mask="aadhaar"
name="aadhaar"
value={aadhaar}
onValueChange={(raw, formatted, complete) => setAadhaar(raw)}
/>
</label>
)
}Use useMask with your own input, or an input from a component library. Spread inputProps on it.
import { useMask } from 'masklet/react'
export function AmountField() {
const { inputProps, raw, complete, valid } = useMask('inr')
return (
<label>
Amount
<input {...inputProps} />
{complete && valid && <small>You entered {raw} rupees.</small>}
</label>
)
}Use Controller so the form stores the raw value. register also works, and then the form stores the formatted value.
import { Controller, useForm } from 'react-hook-form'
import { MaskedInput } from 'masklet/react'
import { isValidPAN } from 'masklet'
export function KycForm() {
const { control, handleSubmit } = useForm({ defaultValues: { pan: '' } })
return (
<form onSubmit={handleSubmit((data) => console.log(data.pan))}>
<Controller
name="pan"
control={control}
rules={{ validate: (value) => isValidPAN(value) || 'Please enter a valid PAN.' }}
render={({ field }) => (
<MaskedInput
mask="pan"
name={field.name}
value={field.value}
onValueChange={(raw) => field.onChange(raw)}
onBlur={field.onBlur}
ref={field.ref}
/>
)}
/>
<button type="submit">Save</button>
</form>
)
}The files in masklet/react start with "use client", so you can use MaskedInput in a Server Component page. The core functions also run on the server, so you can check the value in a Server Action.
import { isValidAadhaar } from 'masklet'
import { MaskedInput } from 'masklet/react'
async function save(formData: FormData) {
'use server'
const aadhaar = String(formData.get('aadhaar'))
if (!isValidAadhaar(aadhaar)) throw new Error('Invalid Aadhaar number')
// save it
}
export default function Page() {
return (
<form action={save}>
<label htmlFor="aadhaar">Aadhaar number</label>
<MaskedInput id="aadhaar" mask="aadhaar" name="aadhaar" required />
<button type="submit">Save</button>
</form>
)
}Check values on the server too
A mask helps while typing. When the form is sent, check the value again. The validators have no browser code, so they also run in Node.js. They ignore spaces and hyphens, and accept lower case letters.
Type anything
no maskisValidAadhaar(value)falseisValidPAN(value)falseisValidGSTIN(value)falseisValidIFSC(value)falseluhn(value)false
import {
isValidAadhaar, isValidPAN, isValidGSTIN, isValidIFSC, luhn,
} from 'masklet'
isValidAadhaar('2345 6789 0124') // true (made up)
isValidAadhaar('2345 6789 0125') // false: wrong check digit
isValidPAN('ABCPK1234Z') // true (made up)
isValidPAN('ABCDE1234F') // false: D is not a holder type
isValidGSTIN('27ABCFK1234Z1ZL') // true (made up)
isValidIFSC('ABCD0001234') // true
luhn('4111 1111 1111 1111') // true (a public test card)
// On the server, check again before you save:
if (!isValidPAN(body.pan)) {
return Response.json({ error: 'Invalid PAN' }, { status: 400 })
}A valid checksum proves the form of a number, not the person. A number that passes may still not exist, or may belong to someone else.
Add it in a minute
No other packages needed. ESM and CommonJS, with TypeScript types. React is optional: you need it only for masklet/react.
npm install maskletpnpm add maskletyarn add maskletbun add maskletPlain JavaScript
<label for="pan">PAN</label>
<input id="pan" type="text">
<script type="module">
import { attachMask } from 'masklet'
const input = document.querySelector('#pan')
const pan = attachMask(input, 'pan', {
onChange: (raw, formatted, complete) => {
console.log(raw, complete)
},
})
pan.valid // true when the PAN passes the checks
pan() // removes the mask; the value stays
</script>React
import { MaskedInput, useMask } from 'masklet/react'
import { format, unformat, validate } from 'masklet'
<MaskedInput mask="phone-in" defaultValue="9876543210" />
format('9876543210', 'phone-in') // '+91 98765 43210'
unformat('+91 98765 43210', 'phone-in') // '9876543210'
validate('29/02/2024', 'date-dmy') // true (a leap year)The full guide, with every option and the API, is in the README.
Your data stays here
Everything runs in your browser. Nothing you type is sent anywhere. Do not type real ID numbers into any demo site; use the fake examples.
This page makes no requests to other sites: no CDN, no web fonts, no analytics and no cookies. masklet is loaded from this site, as one small file. The links to GitHub and npm are plain links.