7 May 20257 minutes to read
This section explains how to create a simple MaskedTextBox component and configure its available functionalities in TypeScript, using Essential® JS 2quickstart seed repository.
This application is integrated with the
webpack.config.jsconfiguration and uses the latest version of thewebpack-cli. It requires nodev14.15.0or higher. For more information about webpack and its features, refer to thewebpack documentation.
The following list of dependencies are required to use the MaskedTextBox component in your application.
|--@syncfusion/ej2-inputs|--@syncfusion/ej2-baseOpen the command prompt from the required directory, and run the following command to clone the Syncfusion® JavaScript (Essential® JS 2) quickstart project fromGitHub.
git clone https://github.com/SyncfusionExamples/ej2-quickstart-webpack- ej2-quickstartAfter cloning the application in theej2-quickstart folder, run the following command line to navigate to theej2-quickstart folder.
cd ej2-quickstartSyncfusion® JavaScript (Essential® JS 2) packages are available on thenpmjs.com public registry. You can install all Syncfusion® JavaScript (Essential® JS 2) controls in a single@syncfusion/ej2 package or individual packages for each control.
The quickstart application is preconfigured with the dependent@syncfusion/ej2 package in the~/package.json file. Use the following command to install the dependent npm packages from the command prompt.
npm installTo render MaskedTextBox component, need to import inputs and its dependent components styles as given below in the~/src/styles/styles.css file, as shown below:
@import'../../node_modules/@syncfusion/ej2-base/styles/material.css';@import'../../node_modules/@syncfusion/ej2-buttons/styles/material.css';@import'../../node_modules/@syncfusion/ej2-popups/styles/material.css';@import'../../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';@import'../../node_modules/@syncfusion/ej2-inputs/styles/material.css';Add the HTML input element that needs to be rendered as MaskedTextBox in theindex.html file.
[src/index.html]
<!DOCTYPE html><htmllang="en"><head><title>Essential JS 2 MaskedTextBox</title><metacharset="utf-8"/><metaname="viewport"content="width=device-width, initial-scale=1.0, user-scalable=no"/><metaname="description"content="Essential JS 2 MaskedTextBox"/><metaname="author"content="Syncfusion"/><linkrel="shortcut icon"href="resources/favicon.ico"/><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"rel="stylesheet"/></head><body><divstyle="margin: 50px;"><!--input element which needs to be rendered/converted as MaskedTextBox--><inputid="mask"type="text"/></div></body></html>Import the MaskedTextBox component into yourapp.ts and append it to the element#mask as shown in the following.
[src/app/app.ts]
import{MaskedTextBox}from'@syncfusion/ej2-inputs';// initializes the MaskedTextBox componentletmask:MaskedTextBox=newMaskedTextBox();mask.appendTo('#mask');You can set the mask to the MaskedTextBox to validate the user input by using themask property. To know more about the usage of mask and configuration, refer to thislink.
The following example demonstrates the usage of mask element0 that allows any single digit from0 to9.
import{MaskedTextBox}from'@syncfusion/ej2-inputs';// initializes the MaskedTextBox componentletmask:MaskedTextBox=newMaskedTextBox({// sets mask format to the MaskedTextBoxmask:'000-000-0000'});mask.appendTo('#mask');Use thenpm run start command to run the application in the browser.
npm run startThe following example shows the MaskedTextBox with the mask element0.
import{MaskedTextBox}from'@syncfusion/ej2-inputs';// initializes the MaskedTextBox componentletmask:MaskedTextBox=newMaskedTextBox({// sets mask format to the MaskedTextBoxmask:'000-000-0000',placeholder:'MaskedTextBox',floatLabelType:'Always'});mask.appendTo('#mask');<!DOCTYPE html><htmllang="en"><head><title>EJ2 MaskedTextBox</title><metacharset="utf-8"/><metaname="viewport"content="width=device-width, initial-scale=1.0"/><metaname="description"content="TypeScript MaskedTextBox Component"/><metaname="author"content="Syncfusion"/><linkhref="styles.css"rel="stylesheet"/><linkhref="https://cdn.syncfusion.com/ej2/31.2.2/ej2-base/styles/material.css"rel="stylesheet"/><linkhref="https://cdn.syncfusion.com/ej2/31.2.2/ej2-inputs/styles/material.css"rel="stylesheet"/><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script><scriptsrc="systemjs.config.js"></script><scriptsrc="https://cdn.syncfusion.com/ej2/syncfusion-helper.js"type="text/javascript"></script></head><body><divid='loader'>Loading....</div><divid='container'><divclass='wrap'><inputid="mask"type="text"/></div></div></body></html>#container{visibility:hidden;}#loader{color:#008cff;font-family:'Helvetica Neue','calibiri';font-size:14px;height:40px;left:45%;position:absolute;top:45%;width:30%;}.wrap{margin:0auto;width:240px;}.label{font-family:'Helvetica Neue','Helvetica','Arial','sans-serif';font-size:14px;}