JS之获取银行卡信息
在使用Lnhn算法验证完银行正确性后,某个功能可能需要显示该银行看的专有信息,例如银行卡所属类型,所属银行等等。
下面使用js实现:
function bankCardAttribution(bankCard) {
var cardTypeMap = {
DC : "储蓄卡",
CC : "信用卡",
SCC : "准贷记卡",
PC : "预付费卡"
};
/*解决多重数组对象的返回结果*/
function extend(target, source) {
var result = {};
var key;
target = target || {};//系统自己产生一个事件对象
source = source || {};//系统自己产生一个事件对象
/*判断对象是否有某个特定的属性。必须用字符串指定该属性。(例如,o.hasOwnProperty("name"))*/
for (key in target) {
if (target.hasOwnProperty(key)) {
result[key] = target[key];
}
}
for (key in source) {
if (source.hasOwnProperty(key)) {
result[key] = source[key];
}
}
return result;
}
/*银行卡类型*/
function getCardTypeName(cardType) {
if (cardTypeMap[cardType]) {
return cardTypeMap[cardType]
}
return undefined;
}
/*银行卡所属银行*/
var bankcardList = [
{
bankName : "中国邮政储蓄银行",
bankCode : "PSBC",
patterns : [{
reg : /^(621096|621098|622150|622151|622181|622188|622199|955100|621095|620062|621285|621798|621799|621797|620529|621622|621599|621674|623218|623219)\d{13}$/g,//\d{13} 13位数字 $ 匹配输入字符串的结束位置 /g (全文查找出现的所有匹配字符) 6+13位 = 19
cardType : "DC"
}, {
reg : /^(62215049|62215050|62215051|62218850|62218851|62218849)\d{11}$/g,// 6+11位 = 17
cardType : "DC"
}, {
reg : /^(622812|622810|622811|628310|625919)\d{10}$/g, //6+10位=16
cardType : "CC"
}]
},
{
bankName : "中国工商银行",
bankCode : "ICBC",
patterns : [
{
reg : /^(620200|620302|620402|620403|620404|620406|620407|620409|620410|620411|620412|620502|620503|620405|620408|620512|620602|620604|620607|620611|620612|620704|620706|620707|620708|620709|620710|620609|620712|620713|620714|620802|620711|620904|620905|621001|620902|621103|621105|621106|621107|621102|621203|621204|621205|621206|621207|621208|621209|621210|621302|621303|621202|621305|621306|621307|621309|621311|621313|621211|621315|621304|621402|621404|621405|621406|621407|621408|621409|621410|621502|621317|621511|621602|621603|621604|621605|621608|621609|621610|621611|621612|621613|621614|621615|621616|621617|621607|621606|621804|621807|621813|621814|621817|621901|621904|621905|621906|621907|621908|621909|621910|621911|621912|621913|621915|622002|621903|622004|622005|622006|622007|622008|622010|622011|622012|621914|622015|622016|622003|622018|622019|622020|622102|622103|622104|622105|622013|622111|622114|622017|622110|622303|622304|622305|622306|622307|622308|622309|622314|622315|622317|622302|622402|622403|622404|622313|622504|622505|622509|622513|622517|622502|622604|622605|622606|622510|622703|622715|622806|622902|622903|622706|623002|623006|623008|623011|623012|622904|623015|623100|623202|623301|623400|623500|623602|623803|623901|623014|624100|624200|624301|624402|623700|624000)\d{12}$/g,
cardType : "DC"
},
{
reg : /^(622200|622202|622203|622208|621225|620058|621281|900000|621558|621559|621722|621723|620086|621226|621618|620516|621227|621288|621721|900010|623062|621670|621720|621379|621240|621724|621762|621414|621375|622926|622927|622928|622929|622930|622931|621733|621732|621372|621369|621763)\d{13}$/g,
cardType : "DC"
},
{
reg : /^(402791|427028|427038|548259|621376|621423|621428|621434|621761|621749|621300|621378|622944|622949|621371|621730|621734|621433|621370|621764|621464|621765|621750|621377|621367|621374|621731|621781)\d{10}$/g,
cardType : "DC"
},
{
reg : /^(9558)\d{15}$/g,
cardType : "DC"
},
{
reg : /^(370246|370248|370249|370247|370267|374738|374739)\d{9}$/g,
cardType : "CC"
}, {
reg : /^(427010|427018|427019|427020|427029|427030|427039|438125|438126|451804|451810|451811|458071|489734|489735|489736|510529|427062|524091|427064|530970|530990|558360|524047|525498|622230|622231|622232|622233|622234|622235|622237|622239|622240|622245|622238|451804|451810|451811|458071|628288|628286|622206|526836|513685|543098|458441|622246|544210|548943|356879|356880|356881|356882|528856|625330|625331|625332|622236|524374|550213|625929|625927|625939|625987|625930|625114|622159|625021|625022|625932|622889|625900|625915|625916|622171|625931|625113|625928|625914|625986|625925|625921|625926|625942|622158|625917|625922|625934|625933|625920|625924|625017|625018|625019)\d{10}$/g,
cardType : "CC"
},
{
reg : /^(45806|53098|45806|53098)\d{11}$/g,
cardType : "CC"
}, {
reg : /^(622210|622211|622212|622213|622214|622220|622223|622225|622229|622215|622224)\d{10}$/g,
cardType : "SCC"
}, {
reg : /^(620054|620142|620184|620030|620050|620143|620149|620124|620183|620094|620186|620148|620185)\d{10}$/g,
cardType : "PC"
}, {
reg : /^(620114|620187|620046)\d{13}$/g,
cardType : "PC"
}]
}
function getBankNameByBankCode(bankcode) {
for (var i = 0, len = bankcardList.length; i < len; i++) {
var bankcard = bankcardList[i];
if (bankcode == bankcard.bankCode) {
return bankcard.bankName;
}
}
return "";
}
/*获取银行卡的类型和所属银行*/
function _getBankInfoByCardNo(cardNo) {
/*循环银行数组*/
for (var i = 0, len = bankcardList.length; i < len; i++) {
/*获取对应循环中对应的银行*/
var bankcard = bankcardList[i];
/*获取对应循环中对应的银行的发行者识别号码和类型数组*/
var patterns = bankcard.patterns;
/*循环银行的发行者识别号码和类型数组*/
for (var j = 0, jLen = patterns.length; j < jLen; j++) {
/*获取循环银行的发行者识别号码数组对象的发行者识别号码和类型*/
var pattern = patterns[j];
/*通过正则表达式发行者识别号码数组对象的发行者识别号码和类与银行卡匹配*/
if ((new RegExp(pattern.reg)).test(cardNo)) {
/**/
var info = extend(bankcard, pattern);
delete info.patterns;
delete info.reg;
info['cardTypeName'] = getCardTypeName(info['cardType']);
return info;//返回银行卡结果
}
}
}
return 'error';
}
return _getBankInfoByCardNo(bankCard)
}
中间的所诉银行按照数组嵌套方式添加:各个银行的银行卡的类型等等信息;