Vue TextBox - Specify a Mask for the Input
A mask defines a pattern for the TextBox value. You can specify the mask using the mask property. A mask can contain the following elements.
Masking Element | Description |
---|---|
0 | A digit. |
9 | A digit or a space. |
# | A digit, a space, "+" or "-" sign. |
L | A literal. |
C | Any character except space. |
c | Any character. |
A | An alphanumeric. |
a | An alphanumeric or a space. |
You can also define custom masking elements using the maskRules object. Each field of this object defines a single masking element.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TextBox } from 'devextreme-react/text-box';
- const maskRules = {
- // a single character
- S: '$',
- // a regular expression
- H: /[0-9A-F]/,
- // an array of characters
- N: ['$', '%', '&', '@'],
- // a function
- F: (char) => {
- return char == char.toUpperCase();
- }
- }
- class App extends React.Component {
- render() {
- return (
- <TextBox
- maskRules={maskRules}
- mask="SFFFFHN"
- />
- );
- }
- }
- export default App;
The masked value goes to the read-only text property, while its unmasked equivalent goes to the value property.
By default, the UI component uses underscores to designate blanks in the masked value. You can specify another symbol using the maskChar property.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TextBox } from 'devextreme-react/text-box';
- class App extends React.Component {
- render() {
- return (
- <TextBox
- mask="+1 (200) 000-0000"
- maskChar="‒"
- />
- );
- }
- }
- export default App;
If the input value does not match the mask, the TextBox displays an error message specified by the maskInvalidMessage property.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { TextBox } from 'devextreme-react/text-box';
- class App extends React.Component {
- render() {
- return (
- <TextBox
- mask="+1 (200) 000-0000"
- maskInvalidMessage="The input value does not match the mask"
- />
- );
- }
- }
- export default App;
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.