What is restricted-input?
The restricted-input npm package allows developers to restrict the input in HTML input fields based on custom rules. It is useful for enforcing specific formats such as phone numbers, credit card numbers, or any other custom input patterns.
What are restricted-input's main functionalities?
Restrict input to a specific pattern
This feature allows you to restrict the input to a specific pattern. In this example, the input is restricted to a phone number format (999-999-9999).
const RestrictedInput = require('restricted-input');
const inputElement = document.querySelector('#my-input');
new RestrictedInput({
element: inputElement,
pattern: '999-999-9999'
});
Custom placeholder character
This feature allows you to set a custom placeholder character. In this example, the placeholder character is set to '_'.
const RestrictedInput = require('restricted-input');
const inputElement = document.querySelector('#my-input');
new RestrictedInput({
element: inputElement,
pattern: '999-999-9999',
placeholder: '_'
});
Dynamic pattern updates
This feature allows you to update the input pattern dynamically. In this example, the pattern is initially set to a phone number format and then updated to a credit card format.
const RestrictedInput = require('restricted-input');
const inputElement = document.querySelector('#my-input');
const restrictedInput = new RestrictedInput({
element: inputElement,
pattern: '999-999-9999'
});
// Update the pattern dynamically
restrictedInput.updatePattern('9999-9999-9999-9999');
Other packages similar to restricted-input
inputmask
Inputmask is a JavaScript library that creates an input mask. Input masks are a way to constrain data that users enter into form fields. It is more feature-rich compared to restricted-input, offering a wide range of predefined masks and additional options for customization.
cleave.js
Cleave.js is a JavaScript library for formatting input text content when you are typing. It offers a variety of formatting options such as credit card numbers, phone numbers, dates, and more. Cleave.js is similar to restricted-input but provides more built-in formats and easier configuration.
vanilla-text-mask
Vanilla Text Mask is a pure JavaScript input mask library. It allows you to create input masks for various types of data, including phone numbers, dates, and custom patterns. It is similar to restricted-input but offers more flexibility and integration with React, Angular, and other frameworks.
Restricted Input
Allow restricted character sets in input
elements.
Features
- Disallow arbitrary chars based on patterns
- Maintains caret position
- Format/Update on paste
- Works in IE9+
Development
Install dependencies
$ npm i
Watch files and run server
$ npm run development
This will start a server on port 3099
which can be overridden with the PORT
env var.
Run tests
There are unit tests:
$ npm t
Usage
import RestrictedInput from 'restricted-input';
const formattedCreditCardInput = new RestrictedInput({
element: document.querySelector('#credit-card'),
pattern: '{{9999}} {{9999}} {{9999}} {{9999}}'
});
API
options
Key | Type | Description |
---|
element | HTMLInputElement or HTMLTextAreaElement | A valid reference to an input or textarea DOM node |
pattern | RegExp | A regular expression describing the allowed character set you wish for entry into corresponding field. |
Browser Support
Desktop
- Chrome (latest)
- Firefox (17+)
- Safari (8+)
- IE11 (desktop/metro)
- IE10 (desktop/metro)
- IE9
TODO