在 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