New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-input-mask-hook

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-input-mask-hook

React hook for connecting to the input of a strict/non-strict mask.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

react-input-mask-hook

React hook for connecting to the input of a strict/non-strict mask.

Installation

npm i react-input-mask-hook

Parameters

Passes an object with properties as input:

NameRequiredTypeDescription
inputRef+RefObjectRef connected to the input element.
mask+stringMask with which the user interacts.
replace+ Object
key: RegExp
value: RegExp
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-booleanDetermines whether the mask is strict or not.
pattern-stringTo check if a mask matches a regular expression input.pattern.

Return value

Hook returns an object with the following properties:

NameTypeDescription
valuestringValue from input.
errorstringError text from input.validationMessage
isValidbooleanIndicates whether the mask matches the pattern.
onChangefunctiononChange handler.

Example

  • Input type must be equal to text.
  • The strict: true is transmitted if the number of characters in the mask is unchanged. For example, phone, ip address or your custom mask.
  • The pattern is passed if you want to use isValid and error.

Strict mask

  const inputRef = useRef<HTMLInputElement>(null);
  const { value, onChange } = useMask({
    inputRef: inputRef,
    mask: "+7 (123) 123-12-13",
    replace: {
      key: /\d/,
      value: /\d/,
    },
    strict: true,
  });

  return (
    <label>
      <input
        type="text"
        value={value}
        onChange={onChange}
        ref={inputRef}
      />
    </label>
  );

In this example, it makes sense to add a pattern for validation, since some characters may be equal to "_".

  const inputRef = useRef<HTMLInputElement>(null);
  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 (
    <label>
      <input
        type="text"
        value={value}
        onChange={onChange}
        ref={inputRef}
      />
      <span>{error}</span>
    </label>
  );

Non-strict mask

  const inputRef = React.useRef<HTMLInputElement>(null);
  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 (
    <label>
      <input
        type="text"
        value={value}
        onChange={onChange}
        ref={inputRef}
      />
      <span>{error}</span>
    </label>
  );
  const inputRef = React.useRef<HTMLInputElement>(null);
  const { value, onChange, error, isValid } = useMask({
    inputRef: inputRef,
    mask: 'http://*.com',
    replace: {
      key: /\*/,
      value: /[a-z]/,
    },
    pattern: 'http:\\/\\/[a-z]+\\.com'
  });

  return (
    <label>
      <input
        type="text"
        value={value}
        onChange={onChange}
        ref={inputRef}
      />
      <span>{error}</span>
    </label>
  );

Keywords

react

FAQs

Package last updated on 12 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts