Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including cc-validate with all npm packages installed. Try it out:

var ccValidate = require("cc-validate")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

cc-validate v2.0.5

Credit Card validation using luhn algorithm

Credit Card Validation

A credit card validation package that is based on luhn algorithm with some small additions to cover the loopholes of this algorithm.

Supported credit card types

  • Visa
  • MasterCard
  • AMEX
  • Discover
  • JCB
  • Diners Club

Demo

https://cc-validate.firebaseapp.com/

Input

Pass the credit card number as a string var result = isValid('4111111111111111');

Output

An object

result = {
    cardNumber : "4111 1111 1111 1111",  // Formatted credit card string
    cardType : 'Visa',  // credit card Type
    isValid : true,  // Boolean True if card is valid, false if it is invalid
    message : ''  // Success/Failure message
}

A detailed explanation of how the underlying algorithm works can be found in this article : https://link.medium.com/FZZwZ0YyXX

A typical use case in a credit card form to notify the user if the credit card number is entered is invalid

Download

You can install card-validator through npm.

Installation

npm install cc-validate --save

Usage

Javascript


var validate = require('cc-validate');
var result = validate.isValid('4196221438170266');

result = {
    cardNumber : "4196 2214 3817 0266", // Formatted Credit Card String
    cardType : 'Visa',  // Credit Card Type
    isValid : true,  // Boolean True if card is valid, false if it is invalid
    message : 'Credit Card number entered valid'  // Success/Failure message
}

TypeScript

import { isValid } from 'cc-validate';
let result = isValid('4196221438170266');
result = {
    cardNumber : "4196 2214 3817 0266", // Formatted Credit Card String
    cardType : 'Visa',              // Credit Card Type
    isValid : true,         // Boolean True if card is valid, false if it is invalid
    message : 'Credit Card number entered valid' // Success/Failure message
}

Test

npm run test

Major and Recent Changes

V 2.0.5 : Added live demo link

V 2.0.4 : Added validation for Diners Club

V 2.0.0 : Addition of credit card type validation and card number formatting

V 1.0.9 First Stable Version

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free