gpassarelli / parsley.js-credit-card-validator Goto Github PK
View Code? Open in Web Editor NEWA credit card validator for Parsley.js with Brands validations
License: MIT License
A credit card validator for Parsley.js with Brands validations
License: MIT License
" - 5255000000000019" passes validation. Will fork and submit a patch.
/*
//---------------------------------------
// Creditcard validation
//---------------------------------------
(function () {
'use strict';
/**
* Helper function to get credit card brand name from credit card number.
* @param string number The credit card number
* @param boolean check_length Should it check for length
* @return mixed The card brand name or null
*/
window.getCreditCardBrand = function(number, check_length){
var _j, _len1,
card_name = [],
number = number.replace(/[ -]/g, '');
if(!number.length){
return;
}
var check_length = (typeof check_length !== 'undefined') ? check_length : false,
card_types = [
{
name: 'amex',
pattern: /^3[47]/,
valid_length: [15]
},{
name: 'china_union_pay',
pattern: /^(62|88)/,
valid_length: [16, 17, 18, 19]
},{
name: 'dankort',
pattern: /^5019/,
valid_length: [16]
},{
name: 'diners_club_carte_blanche',
pattern: /^30[0-5]/,
valid_length: [14]
},{
name: 'diners_club_international',
pattern: /^(30[0-5]|309|36|38|39)/,
valid_length: [14]
},{
name: 'diners_club_us_and_canada',
pattern: /^(54|55)/,
valid_length: [16]
},{
name: 'discover',
pattern: /^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/,
valid_length: [16]
},{
name: 'jcb',
pattern: /^35(2[89]|[3-8][0-9])/,
valid_length: [16]
},{
name: 'laser',
pattern: /^(6304|670[69]|6771)/,
valid_length: [16, 17, 18, 19]
},{
name: 'maestro',
pattern: /^(5018|5020|5038|6304|6759|676[1-3])/,
valid_length: [12, 13, 14, 15, 16, 17, 18, 19]
},{
name: 'mastercard',
pattern: /^5[1-5]/,
valid_length: [16]
},{
name: 'visa',
pattern: /^4/,
valid_length: [16]
},{
name: 'visa_electron',
pattern: /^(4026|417500|4508|4844|491(3|7))/,
valid_length: [16]
}
];
for (_j = 0, _len1 = card_types.length; _j < _len1; _j++) {
var card = card_types[_j];
if (card.pattern.test(number)) {
if (check_length && card.valid_length.indexOf(number.length) > -1) {
card_name.push(card.name);
break;
}
card_name.push(card.name);
}
}
if(card_name.length) {
return card_name.join(' ');
}
return null;
};
//---------------------------------------
// CreditCard Number Verification
//---------------------------------------
window.Parsley.addValidator('creditcard',
function (value, requirement) {
var digit, n, _ref2, valid, _j, _len1,
sum = 0;
value = value.replace(/[ -]/g, '');
_ref2 = value.split('').reverse();
for (n = _j = 0, _len1 = _ref2.length; _j < _len1; n = ++_j) {
digit = _ref2[n];
digit = +digit;
if (n % 2) {
digit *= 2;
if (digit < 10) {
sum += digit;
} else {
sum += digit - 9;
}
} else {
sum += digit;
}
}
valid = (sum % 10 === 0);
// Checks for specific brands
if(valid && requirement.length){
var valid_cards = requirement.split(','),
valid = false,
card = getCreditCardBrand(value, true).split(' ');
for( var c in card){
if(requirement.indexOf(card[c]) > -1) {
valid = true;
}
}
}
return valid;
},32)
.addMessage('en', 'creditcard', 'This Credit Card number is invalid or this brand is not supported.');
//---------------------------------------
// CCV Verification
//---------------------------------------
window.Parsley.addValidator('cvv',
function (value) {
return /^[0-9]{3,4}$/.test(value);
}, 32)
.addMessage('en', 'cvv', 'This value should be a valid CVV number');
//---------------------------------------
// Expiry Date Verification
//---------------------------------------
window.Parsley.addValidator('expirydate',
function (value) {
var currentTime, expiry, prefix, ref;
if(value.indexOf('/') === -1){
return false;
}
var date = value.split('/'),
month = date[0].trim(),
year = date[1].trim();
if (!/^\d+$/.test(month)) {
return false;
}
if (!/^\d+$/.test(year)) {
return false;
}
if (!(parseInt(month, 10) <= 12)) {
return false;
}
if (year.length === 2) {
prefix = (new Date).getFullYear();
prefix = prefix.toString().slice(0, 2);
year = prefix + year;
}
expiry = new Date(year, month);
currentTime = new Date;
expiry.setMonth(expiry.getMonth() - 1);
expiry.setMonth(expiry.getMonth() + 1, 1);
return expiry > currentTime;
}, 32)
.addMessage('en', 'expirydate', 'This value should be a valid date');
}());
Accessing the method 'addValidator' through ParsleyValidator is deprecated.
So replace 'window.Parsley.addValidator(...)' with window.ParsleyValidator.addValidator
Line 197 should have 'expirydate' rather than 'cvv'
Hi there, there are some upcoming credit card changes coming with Visa having up to 19 digit numbers and some Mastercard numbers starting in the 2 range. Have you got plans to update this library for the upcoming changes?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.