Getting Started with EJ2 JavaScript Input Mask

21 Nov 20265 minutes to read

This section explains the steps required to create a simple Essential® JS 2 Masked textbox and demonstrate the basic usage of the Masked textbox control in a JavaScript application.

Dependencies

The list of dependencies required to use the Masked textbox component in your application is given below:

|-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-base

Setup for local environment

Refer to the following steps to set up your local environment.

Step 1: Create a root folder named my-app for your application.

Step 2: Create a my-app/resources folder to store local scripts and styles files.

Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 Masked textbox control.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and Scripts

Add Syncfusion® Maskedtextbox CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base scripts are required dependency packages for the Maskedtextbox component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/maskedtextbox/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-inputs/dist/global/ej2-inputs.min.js

Add MaskedTextBox to the project

Add an HTML input element to be rendered as MaskedTextBox in your index.html file.

[src/index.html]

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 MaskedTextBox</title>
    <!-- Essential JS 2 Input's's dependent bootstrap5.3 theme -->
     <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/maskedtextbox/index.css" rel="stylesheet" />

  <!-- Essential JS 2 all script -->
  <!-- <script src="//cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script> -->

  <!-- Essential JS 2 Input's's dependent scripts -->
  <script src="//cdn.syncfusion.com/ej2/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
  <script src="//cdn.syncfusion.com/ej2/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
</head>

<body>
    <div style="margin: 50px;">
        <!--input element which needs to be rendered/converted as MaskedTextBox-->
        <input id="mask" type="text" />
    </div>

<script>
// initializes the MaskedTextBox component
var mask = new ej.inputs.MaskedTextBox();

mask.appendTo('#mask');
</script>
</body>

</html>

Set the mask

You can set the mask to the MaskedTextBox to validate the user input by using the mask property. To know more about the usage of mask and configuration, refer to this link.

The following example demonstrates the usage of mask element 0, which allows any single digit from 0 to 9.

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 MaskedTextBox</title>
   <!-- Essential JS 2 Input's's dependent bootstrap5.3 theme -->
    <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/maskedtextbox/index.css" rel="stylesheet" />

  <!-- Essential JS 2 all script -->
  <!-- <script src="//cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script> -->

  <!-- Essential JS 2 Input's's dependent scripts -->
  <script src="//cdn.syncfusion.com/ej2/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
  <script src="//cdn.syncfusion.com/ej2/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
</head>

<body>
    <div style="margin: 50px;">
        <!--input element which needs to be rendered/converted as MaskedTextBox-->
        <input id="mask" type="text" />
    </div>

<script>
// initializes the MaskedTextBox component and sets mask format
var mask = new ej.inputs.MaskedTextBox({mask: '000-000-0000'});

mask.appendTo('#mask');
</script>
</body>

</html>

Run the application

Use the following command to run the application in the browser.

npm run start

The following example demonstrates the MaskedTextBox with the mask element 0.

<!DOCTYPE html>
<html lang="en">

<head>
                
    <title>Essential JS 2 MaskedTextBox</title>
    <!-- Essential JS 2 Input's's dependent bootstrap5.3 theme -->
  <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/maskedtextbox/index.css" rel="stylesheet" />

  <!-- Essential JS 2 all script -->
  <!-- <script src="//cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script> -->

  <!-- Essential JS 2 Input's's dependent scripts -->
  <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
  <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
</head>

<body>
    <div id='container' style="width: 240px; margin: 0 auto;">
            <input id="mask" type="text" />
        </div>

<script>
// initializes the MaskedTextBox component and sets mask format
var mask = new ej.inputs.MaskedTextBox({mask: '000-000-0000', placeholder: 'MaskedTextBox', floatLabelType: 'Always'});

mask.appendTo('#mask');
</script>
</body>

</html>

See Also