React hook for connecting to the input of a strict/non-strict mask.
npm install react-input-mask-hookReact hook for connecting to the input of a strict/non-strict mask.
> npm i react-input-mask-hook
Passes an _object_ with properties as input:
| Name | Required | Type | Description | ||
|---|---|---|---|---|---|
| inputRef | + | RefObject | Ref connected to the input element. | ||
| mask | + | string | Mask with which the user interacts. | ||
| replace | + | Object | Key - regular expression. Which characters in the mask the user can edit. Value - regular expression. Those values that you can change the key to. | ||
| strict | - | boolean | Determines whether the mask is strict or not. | ||
| pattern | - | string | To check if a mask matches a regular expression input.pattern. |
Hook returns an _object_ with the following properties:
| Name | Type | Description |
|---|---|---|
| value | string | Value from input. |
| error | string | Error text from input.validationMessage |
| isValid | boolean | Indicates whether the mask matches the pattern. |
| onChange | function | onChange handler. |
1. Input type must be equal to _text_.
2. The _strict: true_ is transmitted if the number of characters in the mask is unchanged. For example, phone, ip address or your custom mask.
3. The _pattern_ is passed if you want to use _isValid_ and _error_.
``
const inputRef = useRef
const { value, onChange } = useMask({
inputRef: inputRef,
mask: "+7 (123) 123-12-13",
replace: {
key: /\d/,
value: /\d/,
},
strict: true,
});
return (
);
`
---
In this example, it makes sense to add a pattern for validation, since some characters may be equal to "\_".
`
const inputRef = useRef
const { value, onChange, error, isValid } = useMask({
inputRef: inputRef,
mask: "+7 (___) ___-__-__",
replace: {
key: /_/,
value: /\d/,
},
strict: true,
pattern: '\\+\\d\\s\\([\\d]{3}\\)\\s[\\d]{3}-\\d\\d-\\d\\d',
});
return (
);
`
`
const inputRef = React.useRef
const { value, onChange, error, isValid } = useMask({
inputRef: inputRef,
mask: 'pochta@gmail.com',
replace: {
key: /[a-z]/i,
value: /[a-z]/i,
},
pattern: '[a-z]+@[a-z]+\\.(ru|com)'
});
return (
);
`
---
`
const inputRef = React.useRef
const { value, onChange, error, isValid } = useMask({
inputRef: inputRef,
mask: 'http://*.com',
replace: {
key: /\*/,
value: /[a-z]/,
},
pattern: 'http:\\/\\/[a-z]+\\.com'
});
return (
);
``