1 回答
TA贡献1808条经验 获得超4个赞
function ReplacingImage(id) {
console.log(id);
var str = document.getElementById(id).value;
var strTest = str.charAt(0);
console.log(str);
switch(strTest) {
case "3":
document.getElementById("x").src="/common/cc-images/amex.png"
break;
case "4":
document.getElementById("x").src="/common/cc-images/visa.png"
break;
case "5":
document.getElementById("x").src="/common/cc-images/mastercard.png"
break;
case "6":
document.getElementById("x").src="/common/cc-images/discover.png"
break;
case "":
document.getElementById("x").src="/common/cc-images/none.png"
break;
default:
document.getElementById("x").src="/common/cc-images/invalid.png"
break;
}
var strNoSpace = str;
var i;
for(i = 0; i < (strNoSpace.length / "4"); i++) {
strNoSpace = strNoSpace.replace(" ", "");
}
var lastDig = strNoSpace.substring(strNoSpace.length - 1, strNoSpace.length);
if(isNaN(lastDig)) { document.getElementById(id").value = str.substring(0, str.length - 1); }
if((strNoSpace.length > 16 && (strTest == "4" || strTest == "5" || strTest == "6")) || (strNoSpace.length > 15 && (strTest == "3"))) {
document.getElementById(id).value = str.substring(0, str.length - 1);
} else if((strNoSpace.length == "15" && strTest == "3") || (strNoSpace.length == "16" && (strTest == "4" || strTest == "5" || strTest == "6"))) {
value = document.getElementById(id").value;
if (/[^0-9-\s]+/.test(value)) {
document.getElementById(id").style.color = "red";
}
let nCheck = 0, bEven = false;
value = value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {
var cDigit = value.charAt(n),
nDigit = parseInt(cDigit, 10);
if (bEven && (nDigit *= 2) > 9) nDigit -= 9;
nCheck += nDigit;
bEven = !bEven;
}
if((nCheck % 10) != 0) {
document.getElementById(id").style.color = "red";
}
else {
document.getElementById(id).style.color = "green";
}
} else {
document.getElementById(id).style.color = "black";
}
if(strNoSpace.length > 15 && (strTest == "3")) { document.getElementById(id).value = str.substring(0, str.length - 1); }
if(!isNaN(lastDig) && strNoSpace.length != "0" && (strNoSpace.length %
"4") == "0" && strNoSpace.length < "16" && event.keyCode != "8") {
document.getElementById(id).value += " ";
}
}
<input type="text" onkeyup="ReplacingImage(this.id)" title="CC of Customer" id="cc" name="cc" placeholder="###">
- 1 回答
- 0 关注
- 82 浏览
添加回答
举报