在 JavaScript 中验证信用卡号
验证信用卡是使用 HTML 表单收款时的关键要素。本文将解释如何使用 JavaScript 验证信用卡号(以不同的格式)。
金融行业有不同的公司销售信用卡。但信用卡号码系统中并没有统一的格式,而且因公司而异。
我们不确定这里讨论的所有表格都是准确的。有时,公司可能会更改其编号系统。你可以通过更改我们用于不同卡片的正则表达式来轻松更改格式。
以下是一些优质信用卡的格式。
识别信用卡的格式
与任何其他磁条卡一样,信用卡或借记卡号码具有由 ISO/IEC 7812 建立的标识格式。标识符的长度通常为 13 到 19 位,用于多种原因。
身份识别信息包含三个信息。
- IIN(发行人身份号码):这是一个六位数的号码,用于标识发行该号码的机构。银行通常以 4 或 5 开头,因此大多数信用卡都以它开头。
- 帐号:用作唯一身份的六到十二位数字。
- 校验位:一位数用于验证标识符的总数。
在 JavaScript 中使用 Luhn 算法检查信用卡
IBM 科学家 Hans Peter Luhn 创造了这种技术来保护数字 ID 免受意外错误的影响。这是一个简单的算法。
- 从号码的最后一位数字开始。
- 将每个备选数字取一个 double,如果 double 值大于 9,则将余数加 1。
- 将加倍值、余数和每个数字的总和除以 10。
- 如果能被除掉,就是合法的。
以下是用于信用卡验证的 Luhn 算法的 JavaScript 实现。
const validCardNumber = numb => {
const regex = new RegExp("^[0-9]{13,19}$");
if (!regex.test(numb)) {
return false;
}
return luhnck(numb);
}
const luhnck = val => {
let validsum = 0;
let k = 1;
for (let l = val.length - 1; l >= 0; l--) {
let calck = 0;
calck = Number(val.charAt(l)) * k;
if (calck > 9) {
validsum = validsum + 1;
calck = calck - 10;
}
validsum = validsum + calck;
if (k == 1) {
k = 2;
} else {
k = 1;
}
}
return (validsum % 10) == 0;
}
console.log(validCardNumber("0987654321456012"));
输出:
false
请记住,此方法检查格式。你仍然必须在服务器上检查是否存在具有此号码的信用卡。
显示信用卡发卡机构
每家提供信用卡的公司都有一个唯一的识别号码,我们可以使用它来确定这张信用卡属于他们。
例如,这是每个组织的格式。
现在我们已经掌握了这种格式,我们将编写一个 JavaScript 函数来验证信用卡并识别它们的种类。
const validCreditcard = cardnumb => {
const ccErrors = [];
ccErrors[0] = "Unknown card type";
ccErrors[1] = "No card number provided";
ccErrors[2] = "Credit card number is in invalid format";
ccErrors[3] = "Credit card number is invalid";
ccErrors[4] = "Credit card number has an inappropriate number of digits";
ccErrors[5] = "Warning! This credit card number is associated with a scam attempt";
const response = (success, message = null, type = null) => ({
message,
success,
type
});
const validCardnumb = numb => {
const regex = new RegExp("^[0-9]{13,19}$");
if (!regex.test(numb)) {
return false;
}
return luhnCheck(numb);
}
const luhnCheck = val => {
let validsum = 0;
let k = 1;
for (let l = val.length - 1; l >= 0; l--) {
let calck = 0;
calck = Number(val.charAt(l)) * k;
if (calck > 9) {
validsum = validsum + 1;
calck = calck - 10;
}
validsum = validsum + calck;
if (k == 1) {
k = 2;
} else {
k = 1;
}
}
return (validsum % 10) == 0;
}
const cards = [];
cards[0] = {
name: "Visa",
length: "13,16",
prefixes: "4",
checkdigit: true
};
cards[1] = {
name: "MasterCard",
length: "16",
prefixes: "51,52,53,54,55",
checkdigit: true
};
cards[2] = {
name: "DinersClub",
length: "14,16",
prefixes: "36,38,54,55",
checkdigit: true
};
cards[3] = {
name: "CarteBlanche",
length: "14",
prefixes: "300,301,302,303,304,305",
checkdigit: true
};
cards[4] = {
name: "AmEx",
length: "15",
prefixes: "34,37",
checkdigit: true
};
cards[5] = {
name: "Discover",
length: "16",
prefixes: "6011,622,64,65",
checkdigit: true
};
cards[6] = {
name: "JCB",
length: "16",
prefixes: "35",
checkdigit: true
};
cards[7] = {
name: "enRoute",
length: "15",
prefixes: "2014,2149",
checkdigit: true
};
cards[8] = {
name: "Solo",
length: "16,18,19",
prefixes: "6334,6767",
checkdigit: true
};
cards[9] = {
name: "Switch",
length: "16,18,19",
prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
checkdigit: true
};
cards[10] = {
name: "Maestro",
length: "12,13,14,15,16,18,19",
prefixes: "5018,5020,5038,6304,6759,6761,6762,6763",
checkdigit: true
};
cards[11] = {
name: "VisaElectron",
length: "16",
prefixes: "4026,417500,4508,4844,4913,4917",
checkdigit: true
};
cards[12] = {
name: "LaserCard",
length: "16,17,18,19",
prefixes: "6304,6706,6771,6709",
checkdigit: true
};
if (cardnumb.length == 0) {
return response(false, ccErrors[1]);
}
cardnumb = cardnumb.replace(/\s/g, "");
if (!validCardnumb(cardnumb)) {
return response(false, ccErrors[2]);
}
if (cardnumb == '5490997771092064') {
return response(false, ccErrors[5]);
}
let lengthValid = false;
let prefixValid = false;
let cardCompany = "";
for (let l = 0; l < cards.length; l++) {
const prefix = cards[l].prefixes.split(",");
for (let k = 0; k < prefix.length; k++) {
const exp = new RegExp("^" + prefix[k]);
if (exp.test(cardnumb)) {
prefixValid = true;
}
}
if (prefixValid) {
const lengths = cards[l].length.split(",");
for (let k = 0; k < lengths.length; k++) {
if (cardnumb.length == lengths[k]) {
lengthValid = true;
}
}
}
if (lengthValid && prefixValid) {
cardCompany = cards[l].name;
return response(true, null, cardCompany);
}
}
if (!prefixValid) {
return response(false, ccErrors[3]);
}
if (!lengthValid) {
return response(false, ccErrors[4]);
}
return response(true, null, cardCompany);
}
console.log(validCreditcard("4111 1111 4321 1234"));
console.log(validCreditcard("3400 0000 0000 009"));
输出:
{
message: "Credit card number is in the invalid format",
success: false,
type: null
}
{
message: null,
success: true,
type: "AmEx"
}
Shiv is a self-driven and passionate Machine learning Learner who is innovative in application design, development, testing, and deployment and provides program requirements into sustainable advanced technical solutions through JavaScript, Python, and other programs for continuous improvement of AI technologies.
LinkedIn