Skip to content

JavaScript Mask

The JavaScript Input Mask plugin is a versatile component designed to enhance user input experience by providing masking functionality for input elements. It offers comprehensive support for handling special keys, selections, and non-input actions. Additionally, it extends its functionality to support contentEditable DIV elements and introduces new tokens inspired by Excel-like masks.

  • Compatible with popular frameworks like React, Angular, and Vue;
  • Optimized for mobile devices, ensuring usability across various platforms;
  • Provides features such as date picker, time picker, and year-month picker;
  • Offers a range of events for seamless integration with existing systems;
  • Can be utilized as a JavaScript plugin or web component;

The JavaScript Input Mask plugin supports the following tokens for defining input masks. Some tokens can be combined, such as Date-time tokens, to create advanced and Excel-like mask patterns:

MethodDescription
aAny letter
0Number
0 litersNumber
0%Percentage
#,##0Currency
$ #,##0.00Currency with decimal
$ #.##0,00Currency with decimal
dd/mm/yyyyDate
hh:mmTime
yyyyYear four digits
yyYear two digits
mmMonth
ddDay
hh24Hour 24
hhHour 12
miMinutes
ssSeconds
\aLetter "a" (escape for a)
\0Number "0" (escape for 0)
[-]Number signal (- or +)

The JavaScript Input Mask plugin supports Excel-like input masks. Below are some valid tokens that can be used with the mask:

Tokens
0
0.00
0%
0.00%
#,##0
#,##0.00
#,##0;(#,##0)
#,##0;Red
#,##0.00;(#,##0.00)
#,##0.00;Red
d-mmm-yy
d-mmm
dd/mm/yyyy
mmm-yy
h:mm AM/PM
h:mm:ss AM/PM
h:mm
h:mm:ss
m/d/yy h:mm
mm:ss
[h]:mm:ss

Learn how to apply masks to HTML input elements using tokens:















<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>

<label>What is your birthday? <i>(Mask: dd/mm/yyyy)</i></label><br>
<input data-mask='dd/mm/yyyy'><br>

<label>US Format <i>(Mask: US #.##0,00)</i></label><br>
<input data-mask='#.##0,00'><br>

<label>Percent mask <i>(Mask: 0%)</i></label><br>
<input data-mask='0%'><br>

<label>How many liters <i>(Mask: 0.00 liters)</i></label><br>
<input data-mask='0.00 liters'><br>

<label>EURO French Format <i>(Mask: # ##0,00 €)</i></label><br>
<input data-mask='# ##0,00 €'><br>

<label>DateTime <i>yyyy-mm-dd hh24:mi</i></label><br>
<input data-mask='yyyy-mm-dd hh24:mi'><br>

<label>CPF <i>000.000.000-00</i></label><br>
<input data-mask='000.000.000-00'><br>
</html>

You can apply masks to contentEditable elements for user input, such as entering currency prices. For example, to enforce the mask "U$ #.##0,00", follow this HTML code:

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<div contentEditable='true' data-mask='U$ #.##0,00' class="input" style="width: 200px"></div>
</html>

In the following example, a notification is displayed when data in an input field is completed. Upon completion, a property data-completed=true is added to the HTML element.

What is your birthday? dd/mm/yyyy

<html>
<script src="https://jsuites.net/v4/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v4/jsuites.css" type="text/css" />

<p>What is your birthday? <strong>dd/mm/yyyy</strong></p>
<input id='input-date-content-editable' data-mask='dd/mm/yyyy'> <span id='mask-status'></span>

<script>
let log = document.getElementById('mask-status');
document.getElementById('input-date-content-editable').addEventListener('keyup', function(e) {
    log.innerHTML = '';
    if (e.target.getAttribute('data-completed') == 'true') {
        log.innerHTML = 'Complete';
    } else {
        log.innerHTML = 'Incomplete';
    }
})
</script>
</html>

You can programmatically apply a mask to a string using jSuites.mask with the following method:





<html>
<script src="https://jsuites.net/v4/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v4/jsuites.css" type="text/css" />

<label>String without mask<br><input type="text" id="mask-method-text" value="1234.45"/></label><br>
<label>Mask<br><input type="text" id="mask-method-mask" value="#.##0,00"/> </label><br>
<button id="btn" class="jbutton dark">Parse string</button><br><br>
<label>Result<br><input type="text" id="result"/></label>

<script>
var text = document.getElementById('mask-method-text');
var mask = document.getElementById('mask-method-mask');
document.getElementById('btn').addEventListener('click', function() {
    document.getElementById('result').value = jSuites.mask.run(parseFloat(text.value), mask.value);
})
</script>
</html>

The JavaScript Mask uses the same Excel-like tokens as Jspreadsheet, making it easy to apply consistent formatting across standalone forms and spreadsheet cells.