Input mask
An input mask formats what the user types to a pattern, such as a phone number or a date.
Default markup
Use an input mask where the value has a fixed format, such as a phone number, a date or a card number.
To create an input mask, add the data-mask attribute to the input element:
<input
type="text"
name="input-mask"
class="form-control"
data-mask="(00) 0000-0000"
placeholder="(00) 0000-0000"
autocomplete="off"
/>
<label class="form-label">Telephone mask</label>
<label class="form-label" for="mask-phone">Phone number</label>
<input type="text" id="mask-phone" name="input-mask" class="form-control" data-mask="(00) 0000-0000" data-mask-visible="true" placeholder="(00) 0000-0000" autocomplete="off" />Pattern
| Character | Accepts |
|---|---|
0 |
a digit |
a |
a letter |
* |
any character |
\ |
makes the next character a literal, so \0 is a plain 0 |
Every other character is a literal, and Tabler adds it for the user as they type. Use the mask on an input with type="text" or type="tel": a number input cannot hold the formatted value. Paste works too: the value is cut to the pattern and the characters that do not fit are dropped.
<label class="form-label" for="mask-date">Date</label>
<input type="text" id="mask-date" class="form-control mb-3" data-mask="00/00/0000" placeholder="00/00/0000" autocomplete="off" />
<label class="form-label" for="mask-plate">Plate</label>
<input type="text" id="mask-plate" class="form-control" data-mask="aa-0000" data-mask-visible="true" autocomplete="off" />Dates and times
data-mask-type="date" builds a pattern from a format and keeps every part in its range. The parts are YYYY, MM (month), DD, HH, mm (minutes) and ss. A digit that cannot start a valid part gets a 0 in front, so typing 4 in the day gives 04. When the user edits a part in the middle of a filled value, the other parts stay in place: a typed digit replaces the next one and a deleted digit becomes 0. A part that is too high is pulled back to the highest value, and the day stays inside the month, with leap years.
<label class="form-label" for="mask-date-type">Date</label>
<input type="text" id="mask-date-type" class="form-control mb-3" data-mask-type="date" data-mask-format="DD/MM/YYYY" data-mask-visible="true" autocomplete="off" />
<label class="form-label" for="mask-datetime">Date and time</label>
<input type="text" id="mask-datetime" class="form-control" data-mask-type="date" data-mask-format="YYYY-MM-DD HH:mm" data-mask-visible="true" autocomplete="off" /><input type="text" class="form-control" data-mask-type="date" data-mask-format="YYYY-MM-DD HH:mm" />
Ranges
To limit the groups of digits of any other pattern, add data-mask-ranges with one min-max for every group, in order. Here every part of an IP address stays between 0 and 255:
<label class="form-label" for="mask-ip">IP address</label>
<input type="text" id="mask-ip" class="form-control" data-mask="000.000.000.000" data-mask-ranges="0-255 0-255 0-255 0-255" data-mask-visible="true" autocomplete="off" /><input type="text" class="form-control" data-mask="000.000.000.000" data-mask-ranges="0-255 0-255 0-255 0-255" />
In JavaScript, ranges is an array: ranges: [[0, 255], [0, 255], [0, 255], [0, 255]]. A group has a fixed length, so write 001 and not 1.
Numbers
For amounts and other numbers, use data-mask-type="number" instead of a pattern. The user types freely and Tabler groups the thousands, keeps the fraction to scale digits and drops everything that is not a number.
<label class="form-label" for="mask-number">Number</label>
<input type="text" id="mask-number" class="form-control mb-3" data-mask-type="number" data-mask-scale="0" data-mask-thousands-separator=" " placeholder="1 234 567" autocomplete="off" />
<label class="form-label" for="mask-money">Money</label>
<input type="text" id="mask-money" class="form-control mb-3" data-mask-type="number" data-mask-prefix="$" data-mask-thousands-separator="," data-mask-min="0" data-mask-pad-fractional-zeros="true" placeholder="$0.00" autocomplete="off" />
<label class="form-label" for="mask-percent">Percent</label>
<input type="text" id="mask-percent" class="form-control" data-mask-type="number" data-mask-suffix=" %" data-mask-scale="1" data-mask-min="0" data-mask-max="100" placeholder="0 %" autocomplete="off" /><input
type="text"
class="form-control"
data-mask-type="number"
data-mask-prefix="$"
data-mask-thousands-separator=","
data-mask-min="0"
data-mask-pad-fractional-zeros="true"
/>
| Option | Attribute | Default | Description |
|---|---|---|---|
type |
data-mask-type="number" |
None | Turns the mask into a number mask. |
scale |
data-mask-scale |
2 |
Digits after the radix. Use 0 for whole numbers. |
radix |
data-mask-radix |
. |
The fractional separator. |
thousandsSeparator |
data-mask-thousands-separator |
none | The character that groups thousands. |
mapToRadix |
None | , for ., . for , |
Other characters that the user can type as the radix. |
min, max |
data-mask-min, data-mask-max |
none | The range. A min of 0 or more also removes the minus sign. |
prefix, suffix |
data-mask-prefix, data-mask-suffix |
none | Text around the number, such as a currency symbol. |
padFractionalZeros |
data-mask-pad-fractional-zeros |
false |
Adds zeros up to scale when the field loses focus. |
unmaskedValue is the number as plain text, with a . as the radix and no separators, so parseFloat(unmaskedValue) works whatever the format on screen. It can be set too: mask.unmaskedValue = '1234.5' shows 1.234,50 with a , radix. The value attribute is read in the format on screen, so set a number from a server with unmaskedValue when the format uses a . to group thousands. A value above max is cut down to max as the user types, and a value below min is raised to it when the field loses focus.
Options
| Option | Attribute | Default | Description |
|---|---|---|---|
mask |
data-mask |
'' |
The pattern, or a function that gets the unmasked value and returns the pattern. |
lazy |
data-mask-visible="true" |
true |
With data-mask-visible the whole pattern shows before the user types. |
placeholderChar |
data-mask-placeholder-char |
_ |
The character for an empty slot when the pattern is visible. |
type |
data-mask-type |
None | date or number, instead of a pattern. pattern keeps a pattern native when IMask is loaded. |
format |
data-mask-format |
DD/MM/YYYY |
The date format, for type: 'date'. |
ranges |
data-mask-ranges |
none | The range of every group of digits in the pattern. |
tokens |
None | {} |
Extra pattern characters, each with the RegExp one character has to match. |
JavaScript
Tabler creates an InputMask for every element with data-mask, the same way as the Bootstrap components. This is the code that runs:
registerPlugin(InputMask, { selector: SELECTOR_DATA_MASK })Create the component yourself for a field added later. The config object wins over the attributes:
const phone = new tabler.InputMask(document.getElementById('phone'), { mask: '(00) 0000-0000' });
A custom token, here a hexadecimal digit:
new tabler.InputMask(document.getElementById('color'), {
mask: '#HHHHHH',
tokens: { H: /[0-9a-f]/i },
});
A function as the mask changes the pattern while the user types, for example for a card number:
new tabler.InputMask(document.getElementById('card'), {
mask: (value) => (/^3[47]/.test(value) ? '0000 000000 00000' : '0000 0000 0000 0000'),
});
With IMask
Tabler does not need the IMask library any more. If your page still loads it, a data-mask pattern goes to IMask as before, with every IMask option, and the mask getter returns the IMask instance. This keeps existing code working, and is deprecated: it goes away in the next major version.
To use the native mask on a page that loads IMask, add data-mask-type="pattern", or type: 'pattern' in the config object. The date and number types, ranges and tokens are always native.
<input type="text" class="form-control" data-mask="00/00/0000" data-mask-type="pattern" />
Methods and properties
| Name | Description |
|---|---|
value |
The value as shown, with the literals. |
unmaskedValue |
Only what the user typed. Send this one to a server. Set it to change the value from code. |
isComplete |
true when every slot is filled. |
update() |
Re-applies the mask after the value was changed from code. |
dispose() |
Removes the mask and the component from the element. |
getInstance(element) |
Static. Returns the component for the element, or null. |
getOrCreateInstance(element, config) |
Static. Returns the component for the element and creates it when needed. |
Events
The events are triggered on the input.
| Event | Description |
|---|---|
accept.bs.input-mask |
The unmasked value changed. Has value and unmaskedValue. |
complete.bs.input-mask |
The last slot was filled. Has value and unmaskedValue. |
input.addEventListener('complete.bs.input-mask', (event) => {
console.log(event.unmaskedValue);
});
Accessibility
- A mask changes what the field accepts as the user types, which is easy to lose track of without sight. Say the expected format in a hint tied to the field with
aria-describedby. - Keep a real
label. The placeholder shows the pattern, and it disappears as soon as typing starts. - Do not block paste. A masked field that rejects a pasted phone number is a dead end for anyone using a password manager or a switch device.
- Use the right
typeandautocomplete, so the browser can still fill the field and the phone keyboard matches.

