Coder Social home page Coder Social logo

cardy's Introduction

cardy

cardy

A credit card utility belt for browser and Node.js apps

Installation

npm install cardy

Usage

Browser (ES6)

Validation

import cardy from 'cardy';

// Validate a card number
const numberValidationResult = cardy.number.validate('4111111111111111');

// Validate an expiration date
const expirationValidationResult = cardy.expiration.validate('01/2020');

Formatting

import cardy from 'cardy';

// Format a card number
const formattedNumber = cardy.number.format('4111111111111111');

// Format an expiration date
const formattedExpiration = cardy.expiration.format('01/2020');

Input Masking

import cardy from 'cardy';

// Credit card input field in your app
const numberInput = document.querySelector('#card-number-input');

numberInput.addEventListener('input', (e) => {
  numberInput.value = cardy.number.mask(numberInput.value);
});

// Expiration date input field in your app
const expirationInput = document.querySelector('#card-expiration-input');

expirationInput.addEventListener('input', (e) => {
  expirationInput.value = cardy.expiration.mask(expirationInput.value);
});

// Security code input field in your app
const securityCodeInput = document.querySelector('#card-security-code-input');

securityCodeInput.addEventListener('input', (e) => {
  securityCodeInput.value = cardy.code.mask(securityCodeInput.value);

  // Optionally, enforce the security code length by passing a valid `codeSize`
  // value, which you can obtain from `card.number.validate()`
  securityCodeInput.value = cardy.code.mask(securityCodeInput.value, 3);
});

Node.js

Validation

const cardy = require('cardy');

// Validate a card number
const numberValidationResult = cardy.number.validate('4111111111111111');

// Validate an expiration date
const expirationValidationResult = cardy.expiration.validate('01/2020');

Formatting

const cardy = require('cardy');

// Format a card number
const formattedNumber = cardy.number.format('4111111111111111');

// Format an expiration date
const formattedExpiration = cardy.expiration.format('01/2020');

Credits

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.