JavaScript卡PAN校验位Luhn验证 [英] JavaScript card PAN check digit Luhn verification

查看:107
本文介绍了JavaScript卡PAN校验位Luhn验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下链接中的代码来尝试验证信用卡,但是当我在现场提交错误数据时,我没有收到提醒。

I've used the code from the link below to try and validate a credit card, however I'm not getting an alert when I submit a the wrong data in the field.

在执行Luhn检查前删除空格

我的表格如下:

<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>

也许我使用了错误的代码?

Maybe I'm using the wrong code?

推荐答案

移动 onsubmit =验证(Luhn);

到表格标签并传递表格

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

Like this - note I pass the form and find the number from the form. I also moved the test and return false/return true around

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卡PAN校验位Luhn验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆