GithubHelp home page GithubHelp logo

braintree / restricted-input Goto Github PK

View Code? Open in Web Editor NEW
66.0 19.0 32.0 2.12 MB

Restrict <input>s to certain valid characters (e.g. formatting phone or card numbers)

Home Page: https://braintree.github.io/restricted-input/

License: MIT License

JavaScript 23.24% HTML 7.83% Shell 0.46% TypeScript 68.48%

restricted-input's Introduction

Restricted Input

Build Status Build Status npm version

Allow restricted character sets in input elements.

Demo

Try the latest version of Restricted Input here.

Features

  • Disallow arbitrary characters based on patterns
  • Maintains caret position
  • Format/Update on paste
  • Works in IE11+

Development

Install dependencies

nvm use # if you have node version manager installed
npm i

Watch files and run demo server

npm run development

This will start a server on port 3099 which can be overridden with the PORT env var.

Unit tests

The following command will run the linting task and the unit tests.

npm test

Integration tests

We use Browserstack to automate end to end testing on Google Chrome, Safari, Firefox, Microsoft Edge, and Internet Explorer 11.

First, sign up for a free open source Browserstack account.

Copy the .env.example file to .env

cp .env.example .env

Fill in the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environmental variables with your credentials:

BROWSERSTACK_USERNAME=username
BROWSERSTACK_ACCESS_KEY=access_key

To run the integration tests in Safari, Google Chrome, Firefox, IE11 and Microsoft Edge:

npm run development # in another terminal window
npm run test:integration

To run tests in only one browser, prefix the test command with an ONLY_BROWSERS env variable:

# run only in edge browser
ONLY_BROWSERS=edge npm run test:integration

# run only in chrome browser
ONLY_BROWSERS=chrome npm run test:integration

# run only in ie 11 browser
ONLY_BROWSERS=ie npm run test:integration

# run only in safari browser
ONLY_BROWSERS=safari npm run test:integration

# run only in firefox browser
ONLY_BROWSERS=firefox npm run test:integration

To run tests in certain browsers, prefix the test command with an ONLY_BROWSERS env variable, with each browser comma separated:

# run only in edge and chrome browsers
ONLY_BROWSERS=edge,chrome npm run test:integration

To run only certain tests, add the .only property before running the test:

it.only('does something', function () {

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.

Paste Event

If an input is changed via a paste event, you may want to adjust the pattern before input formatting occurs. In this case, pass an onPasteEvent callback:

const formattedCreditCardInput = new RestrictedInput({
  element: document.querySelector('#credit-card'),
  pattern: '{{9999}} {{9999}} {{9999}} {{9999}}',
  onPasteEvent: function (payload) {
    var value = payload.unformattedInputValue;

    if (requiresAmexPattern(value)) {
      formattedCreditCardInput.setPattern('{{9999}} {{999999}} {{99999}}')
    } else {
      formattedCreditCardInput.setPattern('{{9999}} {{9999}} {{9999}} {{9999}}')
    }
  })
});

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.
onPasteEvent Function (optional) A callback function to inspect the unformatted value of the input during a paste event. See Paste Event.

Browser Support

Desktop

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

Browsers Where Formatting is Turned Off Automatically

Old versions of Samsung Android browsers are incompatible with formatting. These will not attempt to intercept the values and format the input.

TODO

  • Improve jsdoc
  • Add example guides/pages

restricted-input's People

Contributors

braintreeps avatar crookedneighbor avatar dependabot[bot] avatar evanhahn avatar gesa avatar intelliot avatar jplukarski avatar koddsson avatar lilaconlee avatar okovpashko avatar oscarleonnogales avatar quinnjn avatar scrum avatar skunkworks avatar vtambourine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

restricted-input's Issues

Safari allows hyphens to be typed

In Safari, typing a hyphen when a hyphen is not part of the pattern results in the hyphen appearing. Typing anything after that erases the hyphen adheres to the restricted input pattern.

It's possible that the key events aren't firing when typing a hyphen.

Add AMD support?

General information

  • Library version: 1.2.5
  • Browser and OS: Any

Issue description

๐Ÿ‘‹ The entry point for this package (main.js) seems to only support CommonJS. Would there be interest in adding AMD support, possibly via a UMD loader?

If there's interest, I can submit a PR ๐Ÿ‘จโ€๐Ÿณ

bug: incorrect value after toggle

General information

  • Library version: 2.0.0
  • Browser and OS: Chrome,Firefox,Safari (latest) on MacOS 10.14.5

Issue description

Bug can look at https://braintree.github.io/restricted-input/

Steps

  1. I enter the value (4111111111111111) in field Card Number (toggle-able)
  2. I press the button toggle twice
  3. and get the formatted value one less character 4111 1111 1111 111

Published artefact includes erroneous code.

General information

  • Library version: 1.2.5
  • Browser and OS: Chrome, OSX

Issue description

The built artifact in the npm registry contains this piece of code:

new RestrictedInput({
  element: document.querySelector('#credit-card'),
  pattern: '{{9999}} {{9999}} {{9999}} {{9999}}'
});

module.exports = RestrictedInput;

This ends up raising a run time exception when the library is imported and the page doesn't include a element with the id credit-card.

Interestingly this piece of code doesn't exist in the actual codebase but rather just the built artefact leading me to believe that it's some sort of publishing slip-up where the latest version wasn't built correctly before being pushed to npm.

If someone could republish with the correctly built artefact that would be ๐Ÿ’ฏ.

Chrome: 'KeyboardEvent.keyIdentifier' is deprecated and will be removed in M54.

Originally Reported by @milesich in braintree/braintree-web#195

While using hosted fields in v3.0.1 I am getting this warning in the console:

'KeyboardEvent.keyIdentifier' is deprecated and will be removed in M54, around October 2016. See https://www.chromestatus.com/features/5316065118650368 for more details.

Google Chrome Version 53.0.2785.46 beta (64-bit)

lib/key-cannot-mutate-value.js and lib/restricted-input.js are the two places that use this property.

bug: input errors in Android Chrome

General information

  • Library version: 3.0.5
  • Browser and OS: Chrome 101.0.4951.61 on Android 10; SM-G960U Build/QP1A.190711.020

Issue description

https://codepen.io/brandon-garcia/pen/poaWzvg

If you load up the above codepen link on android chrome, tested myself with a samsung device, and type "123456"
You will sometimes see "1234 556" where the "5" keypress registered twice. This doesn't always happen but happens often enough that in 1 or 2 refreshes you should be able to get this behavior.

Other issues occasionally observed but not yet replicated via codepen:

  • the "5" keypress doesn't register in the input
  • the "6" keypress doesn't register in the input

Lack of documentation on how patterns usage

Our team has the situation where we need to use restricted-input's module to edit Braintree hosted fields' validation rules (yeah, it's weird, but it happened so and we really need some info).
We haven't found documentation on how to use patterns used for default validation rules, example usage:
this.formatter.setPattern('{{99}} / {{9999}}'); and when we try to use regexps (like /^\d{5}(?:[-\s]\d{4})$/) we've got error Uncaught Error: A valid pattern string is required or if we use it in quotes regexp isn't working as expected and just appears in input on keydown. So, could you, please help us clarify how are patterns intended to use. For example of what info we need, currently our goal is to set pattern for optional characters: it should accept 5 numbers or 5 numbers + hyphen + 4 numbers (postal code format), regexp way to do it is /^\d{5}(?:[-\s]\d{4})$/.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.