var cards = new Array();
cards [0] = {name: "VISA", length: "13,16", prefixes: "4", checkdigit: true};
cards [1] = {name: "VISA_DELTA/ELECTRON", length: "16", prefixes: "417500,4844,4508,4026,4917,4913", checkdigit: true};
cards [2] = {name: "MC", length: "16", prefixes: "51,52,53,54,55", checkdigit: true};
然后是我的职能:
function CardTypes()
{
for (var i=0; i<cards.length; i++) {
if cards.name.Validate(PaymentForm.CardNumber.value)<!--pretty sure this is wrong-->
cardname = cards[i].getCardType();
}
if (cardname.length > 0) {
alert("This looks like a " + cardname + " .");
};
};
提交表单时,它调用Validate函数,该函数又调用包括CardTypes()在内的所有其他函数。我希望能够通过卡前缀确定卡类型,即4 =签证。我很确定我完全弄乱了CardTypes()函数。任何帮助,将不胜感激。我的表单ID是PaymentForm。
答案 0 :(得分:0)
PaymentForm.CardNumber.value
设置为什么?这是卡的整数吗?
var cardNumber = PaymentForm.CardNumber.value;
如果这是正确的,那么你会想要读取所有前缀,例如
for (var i=0; i<cards.length; i++) {
var prefixes= cards[i].prefixes;
var prefixArray = prefixes.split(",");
for(var j=0; j < prefixArray.length; j++) {
if(cardNumber.startsWith(prefixArray[j])) {
alert("Card Type is " + cards[i].name);
}
}
}
//将此方法添加到String
String.prototype.startsWith = function(str){
return (this.indexOf(str) === 0);
}
JSFiddle:http://jsfiddle.net/KDTPX/
答案 1 :(得分:0)
function CardTypes()
{
var givenValue = PaymentForm.CardNumber.value;
for (var i = 0; i < cards.length; i++) {
var cardPrefixes = cards[i].prefixes.split(',');
var leave = false;
for (var j = 0; j < cardPrefixes.length; j++) {
if (givenValue.indexOf(cardPrefixes[j]) === 0) {
return cards[i].name;
}
}
}
}
这应该有用。