[英]Invalid regular expression nothing to repeat for validating credit card number type
I've the following regular expression used for validating credit card number type using javascript 我有以下正则表达式,用于使用javascript验证信用卡号类型
var match = /^(?:(4[0-9]{12}(?:[0-9]{3})?)|(5[1-5][0-9]{14})|?(6(?:011|5[0-9]{2})[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])?[0-9]{11})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$/.exec(cardno);
Previously this code was running perfectly but now it shows the above error. 以前,此代码运行良好,但现在显示上述错误。 Can anyone check it out and make any changes?
任何人都可以签出并进行任何更改吗?
A better method would be using the LUHN test. 更好的方法是使用LUHN测试。 https://en.wikipedia.org/wiki/Luhn_algorithm
https://en.wikipedia.org/wiki/Luhn_algorithm
/*
mod10( cardNumber )
parameters:
this function takes the text string card number and runs the Mod 10 formula on its respective digits.
description:
Mod 10 is the check digit formula for the supported cards these functions attempt to validate.
returns:
this function returns true if the number passes the check digit test.
false otherwise.
*/
function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
var ar = new Array( cardNumber.length );
var i = 0,sum = 0;
for( i = 0; i < cardNumber.length; ++i ) {
ar[i] = parseInt(cardNumber.charAt(i));
}
for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
ar[i] *= 2; // every second digit starting with the right most (check digit)
if( ar[i] > 9 ) ar[i]-=9; // will be doubled, and summed with the skipped digits.
} // if the double digit is > 9, ADD those individual digits together
for( i = 0; i < ar.length; ++i ) {
sum += ar[i]; // if the sum is divisible by 10 mod10 succeeds
}
return (((sum%10)==0)?true:false);
}
在http://www.regex101.com上输入它,它会告诉您该块中的问号: 14})|?(6(
应该删除,因为问号意味着前面的标记是可选的,但是它前面带有不是令牌的or运算符,请注意,这与?:
表示非捕获组不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.