JavaScript 卡 PAN 校验位 Luhn 验证

发布于 2024-11-04 18:56:16 字数 1381 浏览 3 评论 0原文

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

在执行 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>

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

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.

Strip spaces before performing Luhn check

my form is as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可可 2024-11-11 18:56:16

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

move onsubmit="Validate(Luhn);"

to the form tag and pass the form

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)"> 
娇妻 2024-11-11 18:56:16

我来到这个问题,寻找 JavaScript 中的在线卡 PAN 验证器,可以安全地用于验证 PAN 校验位 没有服务器恶意拦截的风险。

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

这是典型的实现:

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/

I came to this question looking for online card PAN validator in javascript that can be safely used to verify PAN check digit without risk of malicious intercept on server.

There are plenty of javascript Luhn implementations at http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#JavaScript and https://sites.google.com/site/abapexamples/javascript/luhn-validation.

Here is typical implementation:

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);
    }
})();

And by googling for "luhn jsfiddle" I've found another ready to use robust online validator:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文