Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-text-mask

Package Overview
Dependencies
Maintainers
3
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-text-mask

React input component that accepts mask pattern

  • 5.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created

What is react-text-mask?

The react-text-mask package is a versatile library for creating input masks in React applications. It allows developers to define patterns for user input, ensuring that the data entered conforms to specific formats such as phone numbers, dates, and credit card numbers.

What are react-text-mask's main functionalities?

Basic Input Masking

This feature allows you to create a masked input for phone numbers. The mask pattern ensures that the input follows the format (123) 456-7890.

```jsx
import React from 'react';
import MaskedInput from 'react-text-mask';

const PhoneInput = () => (
  <MaskedInput
    mask={['(', /\d/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
    placeholder="Enter a phone number"
  />
);

export default PhoneInput;
```

Dynamic Masking

This feature demonstrates how to dynamically change the mask based on the input value. The mask changes from a 4-digit to an 8-digit format as the user types.

```jsx
import React, { useState } from 'react';
import MaskedInput from 'react-text-mask';

const DynamicMaskInput = () => {
  const [mask, setMask] = useState([/\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]);

  const handleChange = (e) => {
    const value = e.target.value;
    if (value.length > 4) {
      setMask([/\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]);
    } else {
      setMask([/\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]);
    }
  };

  return (
    <MaskedInput
      mask={mask}
      placeholder="Enter a value"
      onChange={handleChange}
    />
  );
};

export default DynamicMaskInput;
```

Custom Mask Component

This feature allows you to create a custom mask for currency input. The mask pattern ensures that the input follows the format $123,456.78.

```jsx
import React from 'react';
import MaskedInput from 'react-text-mask';

const CustomMaskInput = () => (
  <MaskedInput
    mask={['$', /\d/, /\d/, /\d/, ',', /\d/, /\d/, /\d/, '.', /\d/, /\d/]}
    placeholder="Enter an amount"
  />
);

export default CustomMaskInput;
```

Other packages similar to react-text-mask

Keywords

FAQs

Package last updated on 01 Oct 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc