缺少仅使用JavaScript的信用卡验证链接。需要至少有两个不同的数字,并且所有数字都不应相同。我无法返回文本中的值,请帮助谢谢。这里有一个exercise的链接。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Credit Card Validation</title>
<meta name="description" content="CHANGE THIS CONTENT DESCRIPTION">
</head>
<body>
<span>Credit Card Number* :</span>
<input class="inputForm" type="number" id="ccn" name="inputStealMoney"
placeholder="Enter Credit Card Number"><span class="restriction" id="CCNSpan"></span>
<button type="button" onclick="CCNSpan">Submit</button>
<br>
<p id="CCNSpan">Text</p>
<script>
function funcCall() {
validFormCCN();
}
function validFormCCN() {
var x, textC;
x = document.getElementById('ccn').value;
//If statement below is blank in the last () paranthesis. It needs to be filled with 'different numbers' to work
if (isNaN(x) && (x%2) ==0 && x.length==16 && ()) {
/*The if statement above is NaN() and x%2==0 is even and x.length is 16 digits and the blank () paranthesis where all the digits cannot be the same with atleast two different digits*/
return true;
}
else {
return false;
}
}
document.getElementById("CCNSpan").innerHTML = textC;
</script>
</body>
</html>
发布于 2018-03-30 12:56:44
考虑使用以下正则表达式模式:
(\d)(?!\1{15})\d{15}
这断言信用号码包含16个数字,并且第一个数字在接下来的15个数字中不会重复。这意味着至少有两个数字是不同的。
var str = "1111111111111111";
var patt = new RegExp("(\\d)(?!\\1{15})\\d{15}");
var res = patt.test(str);
console.log(res);
str = "1111111115111111";
res = patt.test(str);
console.log(res);
当然,在实践中,在生产电子商务站点中,您将使用复杂得多的正则表达式。Visa、MasterCard等公司对信用卡号码的格式有一定的规定。您可以访问this link了解更多信息。
发布于 2018-03-30 13:29:26
您的代码看起来有点像是在尝试实现Luhn algorithm。可以很容易地找到许多语言的实现,包括JavaScript。
var luhn10 = function(a,b,c,d,e) {
for(d = +a[b = a.length-1], e=0; b--;)
c = +a[b], d += ++e % 2 ? 2 * c % 10 + (c > 4) : c;
return !(d%10)
};
然而,并不是所有的信用卡公司都使用Luhn算法。建议使用库函数来验证信用卡号码,例如jQuery Validation Plugin。另一个常见的实现是由Braemoor提供的,可以在here中找到基于正则表达式的JS版本。
function validateCcn(value) {
// Accept only spaces, digits and dashes
if ( /[^0-9 \-]+/.test( value ) ) {
return false;
}
var nCheck = 0,
nDigit = 0,
bEven = false,
n, cDigit;
value = value.replace( /\D/g, "" );
if ( value.length < 13 || value.length > 19 ) {
return false;
}
for ( n = value.length - 1; n >= 0; n-- ) {
cDigit = value.charAt( n );
nDigit = parseInt( cDigit, 10 );
if ( bEven ) {
if ( ( nDigit *= 2 ) > 9 ) {
nDigit -= 9;
}
}
nCheck += nDigit;
bEven = !bEven;
}
return ( nCheck % 10 ) === 0;
}
console.log(validateCcn('1234-1234-1234-1234'));
console.log(validateCcn('378282246310005'));
https://stackoverflow.com/questions/49568460
复制相似问题