Search results

Getting Started

The following section explains the required steps to build the MaskedTextBox component with its basic usage in step-by-step procedure.

Dependencies

The following list of dependencies are required to use the MaskedTextBox component in your application.

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

Installation and configuration

You can use Create-react-app to setup the applications. To install create-react-app run the following command.

npm install -g create-react-app

Start a new project using create-react-app command as follows

create-react-app quickstart --scripts-version=react-scripts-ts

cd quickstart

Adding Syncfusion packages

All the available Essential JS 2 packages are published in npmjs.com public registry. You can choose the component that you want to install. For this application, we are going to use MaskedTextBox component.

To install MaskedTextBox component, use the following command

npm install @syncfusion/ej2-react-inputs –save

Adding MaskedTextBox to the application

Now, you can start adding MaskedTextBox component to the application. We have added MaskedTextBox component in src/App.tsx file using following code.

import * as React from "react";
import * as ReactDOM from "react-dom";
import { MaskedTextBoxComponent } from '@syncfusion/ej2-react-inputs';

// initializes MaskedTextBox component
ReactDOM.render(<MaskedTextBoxComponent>
</MaskedTextBoxComponent>,document.getElementById('maskedContainer'));

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 that allows any single digit from 0 to 9.

import * as React from "react";
import * as ReactDOM from "react-dom";
import { MaskedTextBoxComponent } from '@syncfusion/ej2-react-inputs';

// initializes MaskedTextBox component
// sets mask format to the MaskedTextBox
ReactDOM.render(<MaskedTextBoxComponent mask={'000-000-0000'} >
</MaskedTextBoxComponent>,document.getElementById('maskedContainer'));

Adding CSS reference

Import the MaskedTextBox component’s required CSS references as follows in src/App.css.

@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-react-inputs/styles/material.css";

Note: If you want to refer the combined component styles, please make use of our CRG (Custom Resource Generator) in your application.

Run the application

Use the npm run start command to run the application in the browser.

npm run start

The following example shows the MaskedTextBox.

Source
Preview
index.tsx
index.html
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MaskedTextBoxComponent } from '@syncfusion/ej2-react-inputs';

// initializes MaskedTextBox component
// sets value to the MaskedTextBox
class App extends React.Component<{}, {}> {
    render() {
        return (
          <div>
            <label class='label'>Enter the mobile number</label>
            <MaskedTextBoxComponent mask={'000-000-0000'} >
            </MaskedTextBoxComponent>
          </div>
        );
    }
};
ReactDOM.render(<App />, document.getElementById('maskedContainer'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 React MaskedTextBox</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="React MaskedTextBox Component" />
    <meta name="author" content="Syncfusion" />
    <link href="styles.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-inputs/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div class='wrap'>
        <div id='maskedContainer'>
            <div id='loader'>Loading....</div>
        </div>
    </div>
</body>

</html>

See Also