Aadhaar, PAN, GSTIN, IFSC, PIN code, phone and rupees

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.

$ npm install masklet
14presets
0dependencies
~7.5 KBgzipped
React 19ready
MITlicensed
Why masklet

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.

Presets

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.

Rupee amounts

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)
TypedField shows
999999
10001,000
1000001,00,000
12345678.501,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.

Playground

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
TokenAccepts
0 or 9A digit
aA letter
AA letter, made upper case
*A letter or digit
XA 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 elseFixed. 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.

Caret and paste

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

  1. 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.
  2. Backspace over a space. Put the caret just after a space and press Backspace. The digit before the space is deleted, not the space.
  3. Paste. Copy one of the values on the right and paste it. Both give the same result.

Paste these

format(text, 'aadhaar')
Pasted textField showsActions
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.

React

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>
  )
}
Validators

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 mask
  • isValidAadhaar(value)false
  • isValidPAN(value)false
  • isValidGSTIN(value)false
  • isValidIFSC(value)false
  • luhn(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.

Install

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 masklet

Plain 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.