Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
react-imask
Advanced tools
The react-imask package is a versatile input masking library for React applications. It allows developers to create input fields with predefined formats, ensuring that user input adheres to specific patterns. This is particularly useful for forms that require inputs like phone numbers, dates, credit card numbers, and more.
Basic Input Masking
This feature allows you to create a basic input mask for phone numbers. The mask ensures that the input follows the pattern (000) 000-0000.
import React from 'react';
import IMask from 'react-imask';
function PhoneInput() {
return (
<IMask.Input
mask="(000) 000-0000"
placeholder="(123) 456-7890"
/>
);
}
export default PhoneInput;
Dynamic Masking
This feature demonstrates dynamic masking, where the input mask changes based on the input value. For example, it changes the mask for American Express card numbers.
import React, { useState } from 'react';
import IMask from 'react-imask';
function DynamicMaskInput() {
const [mask, setMask] = useState('0000 0000 0000 0000');
return (
<IMask.Input
mask={mask}
placeholder="Enter your card number"
onAccept={(value, mask) => {
if (value.startsWith('34') || value.startsWith('37')) {
setMask('0000 000000 00000'); // American Express
} else {
setMask('0000 0000 0000 0000'); // Other cards
}
}}
/>
);
}
export default DynamicMaskInput;
Custom Masking with Regular Expressions
This feature allows you to use regular expressions for custom input masking. In this example, the input only accepts lowercase letters.
import React from 'react';
import IMask from 'react-imask';
function RegexMaskInput() {
return (
<IMask.Input
mask={/^[a-z]*$/}
placeholder="Enter lowercase letters only"
/>
);
}
export default RegexMaskInput;
react-text-mask is another popular library for input masking in React. It provides similar functionality to react-imask, allowing developers to create masked input fields. However, react-imask offers more flexibility with dynamic and custom masks.
cleave.js is a JavaScript library for formatting input fields. It supports a wide range of input formats, including credit card numbers, phone numbers, and dates. While cleave.js is not React-specific, it can be easily integrated into React applications. Compared to react-imask, cleave.js is more focused on formatting rather than masking.
inputmask is a versatile library for creating input masks in web applications. It supports a variety of input types and can be used with different frameworks, including React. While inputmask offers similar functionality to react-imask, it may require more configuration for complex masks.
react-imask
npm install react-imask
import { useRef } from 'react';
import { IMaskInput } from 'react-imask';
// use ref to get access to internal "masked = ref.current.maskRef"
const ref = useRef(null);
const inputRef = useRef(null);
<IMaskInput
mask={Number}
radix="."
value="123"
unmask={true} // true|false|'typed'
ref={ref}
inputRef={inputRef} // access to nested input
// DO NOT USE onChange TO HANDLE CHANGES!
// USE onAccept INSTEAD
onAccept={
// depending on prop above first argument is
// `value` if `unmask=false`,
// `unmaskedValue` if `unmask=true`,
// `typedValue` if `unmask='typed'`
(value, mask) => console.log(value)
}
// ...and more mask props in a guide
// input props also available
placeholder='Enter number here'
/>
import { IMaskMixin } from 'react-imask';
// extend style component
const StyledInput = styled.input`
color: green;
`;
const MaskedStyledInput = IMaskMixin(({inputRef, ...props}) => (
<StyledInput
{...props}
innerRef={inputRef} // bind internal input (if you use styled-components V4, use "ref" instead "innerRef")
/>
));
<MaskedStyledInput
mask={Number}
radix="."
onAccept={(value, mask) => console.log(value)}
// ...and more mask props in a guide
// ...other styled props
/>
More options see in a guide.
import { useState } from 'react';
import { useIMask } from 'react-imask';
function IMaskWithHook () {
const [ opts, setOpts ] = useState({ mask: Number });
const {
ref,
maskRef,
value,
setValue,
unmaskedValue,
setUnmaskedValue,
typedValue,
setTypedValue,
} = useIMask(opts, /* { onAccept, onComplete } */);
return (
<input ref={ref} />
);
}
FAQs
React input mask
The npm package react-imask receives a total of 192,328 weekly downloads. As such, react-imask popularity was classified as popular.
We found that react-imask demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.