Restrict s to certain valid characters (e.g. formatting phone or card numbers)
Перейти к файлу
intelliot and richardshin 02ee8c98a0 Update README 2017-01-23 23:01:22 +00:00
config Initial commit 2016-05-27 22:00:09 +00:00
lib Disable restricted input in SamsungBrowser 2017-01-23 22:34:06 +00:00
spec Document patterns with examples (#17) 2016-10-20 22:06:21 -05:00
test Disable restricted input in SamsungBrowser 2017-01-23 22:34:06 +00:00
.eslintignore Initial commit 2016-05-27 22:00:09 +00:00
.eslintrc Initial commit 2016-05-27 22:00:09 +00:00
.gitignore Add sauce_connect.ready to gitignore 2016-08-29 15:54:11 +00:00
.npmignore Add travis 2016-05-27 22:00:28 +00:00
.nvmrc Update to Node 6 2016-11-04 20:32:26 +00:00
.rspec Initial commit 2016-05-27 22:00:09 +00:00
.rvmrc Initial commit 2016-05-27 22:00:09 +00:00
.travis.yml Pend ios travis build 2016-11-15 20:09:43 +00:00
CHANGELOG.md Update README 2017-01-23 23:01:22 +00:00
Gemfile Initial commit 2016-05-27 22:00:09 +00:00
Gemfile.lock Initial commit 2016-05-27 22:00:09 +00:00
LICENSE Update license for 2017 2017-01-13 14:37:47 -08:00
README.md Exit publish script early on errors, https and inline link 2017-01-18 22:43:19 +00:00
Rakefile Initial commit 2016-05-27 22:00:09 +00:00
main.js Initial commit 2016-05-27 22:00:09 +00:00
package.json Add Demo page 2017-01-18 19:58:15 +00:00
publish-gh-pages.sh s/test/demo for gh-pages template commit message 2017-01-18 23:23:22 +00:00

README.md

Restricted Input

Allow restricted character sets in input elements.

Demo

Try the latest version of Restricted Input here.

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}}'
});

Patterns

Patterns are a mixture of Placeholders and PermaChars.

Placeholder

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 single alpha character that matches the alpha regex /[A-Za-z]/. e.g. {{C}} will match one alpha character.
  • a single digit that matches the digit regex /[0-9]/. e.g. {{3}} will match one digit.
  • a * character that matches /./. e.g. {{*}} will match the next character.

PermaChar

A PermaChar is the part of the pattern that is automatically inserted. PermaChars are defined in the pattern as any characters other than Placeholders.

Example patterns

Some example patterns with behavior are listed:

  • 12{{3}}
    • Inserts 12.
    • Waits for a single digit from the user.
  • {{A}}BC
    • Waits for a single alpha from the user.
    • Inserts BC.
  • ${{*2L}}E
    • Inserts $.
    • Waits for any single character input from the user.
    • Waits for a single digit from the user.
    • Waits for a single alpha from the user.
    • Inserts E.

API

options

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.

Browser Support

Desktop

  • Chrome (latest)
  • Firefox (17+)
  • Safari (8+)
  • IE11 (desktop/metro)
  • IE10 (desktop/metro)
  • IE9

TODO

  • Improve jsdoc
  • Add example guides/pages