【问题标题】:JavaScript card PAN check digit Luhn verificationJavaScript 卡 PAN 校验位 Luhn 验证
【发布时间】:2011-08-17 16:41:26
【问题描述】:

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

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>

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

【问题讨论】:

    标签: javascript credit-card luhn


    【解决方案1】:

    移动onsubmit="Validate(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)"> 
    

    【讨论】:

    • 感谢您的回复...我复制了上面的代码,但表格似乎只是提交。
    • @mplugjan - 非常感谢您的帮助,效果很好。
    • 不客气...现在看看我对提交按钮问题的其他回答
    【解决方案2】:

    我来这个问题是在寻找 javascript 中的在线卡 PAN 验证器,它可以安全地用于verify PAN check digit 而不会在服务器上被恶意拦截。

    http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#JavaScripthttps://sites.google.com/site/abapexamples/javascript/luhn-validation 有大量 javascript Luhn 实现。

    这是典型的实现:

    var LuhnCheck = (function()
    {
        var luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
        return function(str)
        {
            var counter = 0;
            var incNum;
            var odd = false;
            var temp = String(str).replace(/[^\d]/g, "");
            if ( temp.length == 0)
                return false;
            for (var i = temp.length-1; i >= 0; --i)
            {
                incNum = parseInt(temp.charAt(i), 10);
                counter += (odd = !odd)? incNum : luhnArr[incNum];
            }
            return (counter%10 == 0);
        }
    })();
    

    通过谷歌搜索“luhn jsfiddle”,我发现了另一个可以使用的强大在线验证器:

    http://jsfiddle.net/silvinci/84bru/light/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 2019-11-25
      相关资源
      最近更新 更多