简体   繁体   English

无效的正则表达式,无需重复任何操作即可验证信用卡号类型

[英]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.

相关问题 正则表达式Javascript-无效的正则表达式:数字输入无需重复 - Regex Javascript - Invalid regular expression: Nothing to repeat for number input SyntaxError:无效的正则表达式:无需重复 - SyntaxError: Invalid regular expression: nothing to repeat 未捕获的SyntaxError:无效的正则表达式:无需重复 - Uncaught SyntaxError: Invalid regular expression: Nothing to repeat 无效的正则表达式:无重复错误 - Invalid Regular Expression: Nothing to Repeat Error Javascript regexp错误(无效的正则表达式:/?/:无需重复) - Javascript regexp error (Invalid regular expression: /?/: Nothing to repeat) Javascript .search()问题Uncaught SyntaxError:无效的正则表达式:/ * /:无需重复 - Javascript .search() issue Uncaught SyntaxError: Invalid regular expression: /*/: Nothing to repeat Node.js正则表达式错误:“无效的正则表达式:无可重复” - Nodejs regexp error: “Invalid regular expression: nothing to repeat” 无效的正则表达式:没有可重复的 Safari 控制台错误 - Invalid regular expression: nothing to repeat Safari Console Error 摩纳哥编辑器未捕获的语法错误:无效的正则表达式:/^*-+:*$/:无须重复 - Monaco editor Uncaught SyntaxError: Invalid regular expression: /^*-+:*$/: Nothing to repeat 在 Jest 中使用正则表达式验证被屏蔽的信用卡号 - Validating masked credit card number with regex in Jest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM