Giter VIP home page Giter VIP logo

credit-card-input-mask's Introduction

credit-card-input-mask

forked from braintree/restricted-input

Allow restricted character sets in input elements.

Install

$ npm install --save credit-card-input-mask

Usage

import CreditCardInputMask from "credit-card-input-mask";

const formattedCreditCardInput = new CreditCardInputMask({
  element: document.querySelector("#credit-card"),
  pattern: "{{9999}} {{9999}} {{9999}} {{9999}}",
});

Demo

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.

Related

credit-card-input-mask's People

Stargazers

 avatar

Watchers

 avatar  avatar

credit-card-input-mask's Issues

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.