UNPKG

credit-card-validator

Version:

Provides Credit Card Validation

18 lines 5.8 kB
!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.creditCardValidator=f()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){/* * @license Copyright (C) 2015 Michael Dempsey * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * Lesser GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <https://www.gnu.org/licenses/lgpl.html>. * */ var map=Array.prototype.map,creditCardTypes=[{cardName:"visa",codeLengths:[{accountNum:16,securityNum:[3]}],regex:/^4/,faClass:"fa fa-cc-visa"},{cardName:"americanexpress",codeLengths:[{accountNum:15,securityNum:[4]}],regex:/^3[47]/,faClass:"fa fa-cc-amex"},{cardName:"mastercard",codeLengths:[{accountNum:16,securityNum:[3]}],regex:/^5[1-5]/,faClass:"fa fa-cc-mastercard"},{cardName:"discover",codeLengths:[{accountNum:16,securityNum:[3]}],regex:/^6011|^62212[6-9]|^6221[3-9][0-9]|^622[2-8][0-9][0-9]|^6229[0-1][0-9]|^62292[0-5]|^64[4-9]|^65/,faClass:"fa fa-cc-discover"},{cardName:"JCB",codeLengths:[{accountNum:16,securityNum:[3]}],regex:/^352[89]|^35[3-8][0-9]/,faClass:"fa fa-credit-card"}],getNumberString=function(string){var matches=string.match(/[0-9]+/g);return matches?matches.join(""):""};Array.prototype.unique=function(){for(var arrOut=[],i=0,arrLen=this.length;arrLen>i;i++)arrOut.indexOf(this[i])<0&&arrOut.push(this[i]);return arrOut};var luhnCheck=function(numberString){function processDigit(num){return num*=2,10>num?num:num%10+Math.floor(num/10)}for(var intArr=map.call(numberString,function(char){return parseInt(char,10)}),arrLen=intArr.length,oddOrEven=arrLen%2,checksum=0,index=0;arrLen>index;index++)checksum+=index%2===oddOrEven?processDigit(intArr[index]):intArr[index];return checksum%10===0};exports.getCardName=function(accountString){for(var accountNumberString=getNumberString(accountString),cardName="",type=0,numTypes=creditCardTypes.length;numTypes>type;type++)if(creditCardTypes[type].regex.test(accountNumberString)){cardName=creditCardTypes[type].cardName;break}return cardName},exports.getFaClass=function(accountString){for(var accountNumberString=getNumberString(accountString),faClass="",type=0,numTypes=creditCardTypes.length;numTypes>type;type++)if(creditCardTypes[type].regex.test(accountNumberString)){faClass=creditCardTypes[type].faClass;break}return faClass},exports.getCardAccountNumLengths=function(accountString){for(var accountNumberString=getNumberString(accountString),typeLengths=[-1],type=0,numTypes=creditCardTypes.length;numTypes>type;type++)if(creditCardTypes[type].regex.test(accountNumberString)){var codeLengths=creditCardTypes[type].codeLengths;typeLengths=codeLengths.map(function(elem){return elem.accountNum});break}return typeLengths},exports.getCardSecurityNumLengths=function(accountString){for(var accountNumberString=getNumberString(accountString),typeLengths=[-1],numLength=accountNumberString.length,type=0,numTypes=creditCardTypes.length;numTypes>type;type++)if(creditCardTypes[type].regex.test(accountNumberString)){for(var codeLengths=creditCardTypes[type].codeLengths,allSecurityCodeLengths=[],i=0,arrLen=codeLengths.length;arrLen>i;i++){if(allSecurityCodeLengths=allSecurityCodeLengths.concat(codeLengths[i].securityNum),codeLengths[i].accountNum===numLength){typeLengths=codeLengths[i].securityNum;break}typeLengths[0]<0&&i===arrLen-1&&(typeLengths=allSecurityCodeLengths.unique())}break}return typeLengths},exports.validateCardLength=function(accountString){var accountNumberString=getNumberString(accountString),baseTest=/^[0-9]{15,16}$/;if(!baseTest.test(accountNumberString))return!1;var numLength=accountNumberString.length,typeLengths=exports.getCardAccountNumLengths(accountNumberString);return typeLengths[0]<0||typeLengths.indexOf(numLength)<0?!1:!0},exports.validateCardSecurityCodeLength=function(accountString,securityString){var accountNumberString=getNumberString(accountString),securityNumberString=getNumberString(securityString),baseTest=/^[0-9]{3,4}$/;if(!baseTest.test(securityString))return!1;var numLength=securityNumberString.length,typeLengths=exports.getCardSecurityNumLengths(accountNumberString);return typeLengths[0]<0||typeLengths.indexOf(numLength)<0?!1:!0},exports.validateCardLuhn=function(accountString){var accountNumberString=getNumberString(accountString);return luhnCheck(accountNumberString)},exports.validateCard=function(accountString){return exports.validateCardLength(accountString)&&exports.validateCardLuhn(accountString)},exports.validateCardAndSecCode=function(accountString,securityString){return exports.validateCardLength(accountString)&&exports.validateCardLuhn(accountString)&&exports.validateCardSecurityCodeLength(accountString,securityString)}},{}]},{},[1])(1)});