Validate credit card by using jquery
How can i validate credit/debit card by using jquery/javascript?
I have tried to do by using Luhn algorithm.
function checkLuhn(input) {
var sum = 0;
var numdigits = input.length;
var parity = numdigits % 2;
for(var i=0; i < numdigits; i++) {
var digit = parseInt(input.charAt(i))
if(i % 2 == parity) digit *= 2;
if(digit > 9)
digit -= 9;
sum += digit;
}
return (sum % 10) == 0;
}
您可以防止用户使用jQuery Mask Plugin首先输入不正确的值
$('#debit').mask('0000 0000 0000 0000');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<input type="text" id="debit">
This is how I did it with Luhn-algorithm and card number length (we only support specific cards that have 16 digits):
// Luhn-algorithm check
function checkCardNumber(number) {
var sumTable = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
],
sum = 0,
flip = 0,
i;
for (i = number.length - 1; i >= 0; i--) {
sum += sumTable[flip++ & 0x1][parseInt(number.charAt(i), 10)];
}
return sum % 10 === 0;
}
// Check if the VISA card input is valid
var regCardNo = (opts.cardData ? /^[0-9]{6}[*]{6}[0-9]{4}$/ : /^[0-9]{16}$/),
regMonth = /^[0]{1}[1-9]{1}$|^[1]{1}[0-2]{1}$/,
regYear = /^[0]{1}[1-9]{1}$|^[1-9]{1}[0-9]{1}$/,
regCvc = /^[0-9]{3}$/,
cardNo = cardNumberInput.val(),
month = monthInput.val(),
year = yearInput.val(),
cvc = cvcInput.val();
if(regCardNo.test(cardNo) && regMonth.test(month) && regYear.test(year) && regCvc.test(cvc) && checkCardNumber(cardNo)) {
payButton.prop("disabled", false);
return true;
} else {
payButton.prop("disabled", true);
return false;
}
All input variables are jQuery elems (they are not defined in this code snippet )
NOTE that the opts.cardData checks if you have a prefilled card number (which gets loaded from the DB), then it checks for a masked number (ex. 123456******1234), else it checks for a full length number which you add in the form your self.
链接地址: http://www.djcxy.com/p/70714.html上一篇: 用于信用卡检测的RegEx和Keyup
下一篇: 使用jquery验证信用卡