编程语言
首页 > 编程语言> > JavaScript卡PAN校验位Luhn验证

JavaScript卡PAN校验位Luhn验证

作者:互联网

我已经使用下面链接中的代码尝试验证信用卡,但是当我在现场提交错误数据时,我没有收到警报.

Strip spaces before performing Luhn check

我的表格如下:

<form id="myform" method="post" action=""> 

<p>Select credit card:
   <select tabindex="11" id="CardType"> 
      <option value="AmEx">American Express</option> 
    <option value="CarteBlanche">Carte Blanche</option> 
    <option value="DinersClub">Diners Club</option> 
    <option value="Discover">Discover</option> 
    <option value="EnRoute">enRoute</option> 
    <option value="JCB">JCB</option> 
    <option value="Maestro">Maestro</option> 
    <option value="MasterCard">MasterCard</option> 
    <option value="Solo">Solo</option> 
    <option value="Switch">Switch</option> 
    <option value="Visa">Visa</option> 
    <option value="VisaElectron">Visa Electron</option> 
    <option value="LaserCard">Laser</option> 
  </select> 
</p>

<p>
Enter number:
 <input type="text" id="CardNumber" maxlength="24" size="24" />
  <input type="submit" id="submitbutton" onsubmit="Validate(Luhn);" />  
</p> 

</form>

也许我使用错误的代码?

解决方法:

move onsubmit =“验证(Luhn);”

到表单标签并传递表单

像这样 – 注意我传递表单并从表单中找到数字.
我也移动了测试并返回false / return true

http://jsfiddle.net/mplungjan/VqXss/

function Validate(theForm) {
  var Luhn = theForm.CardNumber.value;
  var LuhnDigit = parseInt(Luhn.substring(Luhn.length-1,Luhn.length));
  var LuhnLess = Luhn.substring(0,Luhn.length-1);
  if (Calculate(LuhnLess)!=parseInt(LuhnDigit)) {
    alert("\n\nYou have mis-typed your card number! \nPlease check and correct.\n\n")   
    return false;
  }
  return true;
}
</script>
</head>
<body>
<form id="myform" method="post" action="" onsubmit="return Validate(this)"> 

标签:javascript,credit-card,luhn
来源: https://codeday.me/bug/20190630/1339209.html