![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
restricted-input
Advanced tools
Restrict inputs to certain valid characters (e.g. formatting phone or card numbers)
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.
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');
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 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 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.
Allow restricted character sets in input
elements.
Try the latest version of Restricted Input here.
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
import RestrictedInput from 'restricted-input';
const formattedCreditCardInput = new RestrictedInput({
element: document.querySelector('#credit-card'),
pattern: '{{9999}} {{9999}} {{9999}} {{9999}}'
});
Patterns are a mixture of Placeholder
s and PermaChar
s.
A Placeholder
is the part of the pattern that accepts user input based on some restrictions. A placeholder is defined in the pattern using two open curly brackets, the placeholder, followed by two closing curly brackets e.g. {{Abc123}}
.
The patterns a Placeholder
can be are:
/[A-Za-z]/
. e.g. {{C}}
will match one alpha character./[0-9]/
. e.g. {{3}}
will match one digit.*
character that matches /./
. e.g. {{*}}
will match the next character.A PermaChar
is the part of the pattern that is automatically inserted. PermaChar
s are defined in the pattern as any characters other than Placeholder
s.
Some example patterns with behavior are listed:
12{{3}}
12
.{{A}}BC
BC
.${{*2L}}E
$
.E
.Key | Type | Description |
---|---|---|
element | HTMLInputElement or HTMLTextAreaElement | A valid reference to an input or textarea DOM node |
pattern | String | Pattern describing the allowed character set you wish for entry into corresponding field. See Patterns. |
Desktop
Old versions of Samsung Android browsers are incompatible with formatting. These will not attempt to intercept the values and format the input.
1.2.0 (2017-04-08)
FAQs
Restrict inputs to certain valid characters (e.g. formatting phone or card numbers)
The npm package restricted-input receives a total of 119,096 weekly downloads. As such, restricted-input popularity was classified as popular.
We found that restricted-input demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.