cc-validate
Version:
Credit Card validation using luhn algorithm
97 lines (73 loc) • 2.23 kB
Markdown
A credit card validation package that is based on luhn algorithm with some small additions to cover the loopholes of this algorithm.
- Visa
- MasterCard
- AMEX
- Discover
- JCB
- Diners Club
https://cc-validate.firebaseapp.com/
Pass the credit card number as a string
var result = isValid('4111111111111111');
An object
```sh
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
You can install `card-validator` through `npm`.
```sh
npm install cc-validate --save
```
```javascript
var validate = require('cc-validate');
var result = validate.isValid('4196221438170266');
```
```sh
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');
```
```sh
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
}
```
```sh
npm run test
```
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