42 是有效的信用卡号码吗? jQuery 验证器认为是

发布于 2024-08-14 11:05:28 字数 464 浏览 5 评论 0原文

我们都知道 42 是一切问题的答案,但对我来说这是个新闻有效的信用卡号码。

尝试在此jQuery 验证测试页面中输入“42”或“42176”,然后单击好的。

这是怎么回事?我认为这应该是事实上的验证库。连微软现在都在使用它,但它认为“42”和“42176”是有效的信用卡号码?!它甚至没有进行长度检查。当然我也不负责添加长度检查吗?它被称为“信用卡”验证器,而不是“luhn”验证器。

编辑:抛开搭便车幽默 - 我将如何修补验证插件以检查长度。是那么容易吗?

We all know that 42 is the answer to everything, but it's news to me that it is a valid credit card number.

Try entering '42' or '42176' into this jQuery Validation testing page and click OK.

What's going on? I thought this was supposed to be the de-facto validation library. Even Microsoft is using it now, but it thinks '42' and '42176' are valid credit card numbers?! It's not even doing a length check. Surely I'm not responsible for adding a length check too? It's called 'creditcard' validator and not 'luhn' validator.

Edit: hitchhiker humor aside - how would I go about patching the validation plugin to check length. is that easy?

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

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

发布评论

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

评论(4

飞烟轻若梦 2024-08-21 11:05:28

这可能是因为此验证器仅检查提供的数字是否满足 LUHN-10< /a> 算法(42 满足,因为 4*2 + 2 = 10,即 0 模 10)。

更好的验证器可能应该检查最少数量的数字。

我不确定这是否对应于 jQuery 的最新代码,但我找到了与信用卡验证相关的代码片段:

    // http://docs.jquery.com/Plugins/Validation/Methods/creditcard
    // based on http://en.wikipedia.org/wiki/Luhn
    creditcard: function(value, element) {
        if ( this.optional(element) )
            return "dependency-mismatch";
        // accept only digits and dashes
        if (/[^0-9-]+/.test(value))
            return false;
        var nCheck = 0,
            nDigit = 0,
            bEven = false;

        value = value.replace(/\D/g, "");

        for (n = value.length - 1; n >= 0; n--) {
            var cDigit = value.charAt(n);
            var nDigit = parseInt(cDigit, 10);
            if (bEven) {
                if ((nDigit *= 2) > 9)
                    nDigit -= 9;
            }
            nCheck += nDigit;
            bEven = !bEven;
        }

        return (nCheck % 10) == 0;
    },

...正如您所看到的,这只是检查所有字符是否都是数字并且 LUHN-10 是否满足,而不需要对最小长度的任何关注。

This is probably because this validator merely checks that the number supplied satisfies the LUHN-10 algorithm (which 42 satisfies since 4*2 + 2 = 10 which is 0 modulo 10).

A better validator should maybe check for a minimal number of digits.

I'm not sure this corresponds to the very latest code from jQuery, but I found the snippet associated with credit card validation:

    // http://docs.jquery.com/Plugins/Validation/Methods/creditcard
    // based on http://en.wikipedia.org/wiki/Luhn
    creditcard: function(value, element) {
        if ( this.optional(element) )
            return "dependency-mismatch";
        // accept only digits and dashes
        if (/[^0-9-]+/.test(value))
            return false;
        var nCheck = 0,
            nDigit = 0,
            bEven = false;

        value = value.replace(/\D/g, "");

        for (n = value.length - 1; n >= 0; n--) {
            var cDigit = value.charAt(n);
            var nDigit = parseInt(cDigit, 10);
            if (bEven) {
                if ((nDigit *= 2) > 9)
                    nDigit -= 9;
            }
            nCheck += nDigit;
            bEven = !bEven;
        }

        return (nCheck % 10) == 0;
    },

... and as you see this merely check that all characters are digits and that LUHN-10 is satisfied, without any attention to a minial length.

不再让梦枯萎 2024-08-21 11:05:28

您可以将信用卡规则与最小和最大长度规则结合起来以实现您想要的。这可能看起来需要付出太多的努力——我可能同意——尽管如果您只想接受某些卡号长度,它确实会给您更多的控制权。

$('form').validate({
    '#ccNum': {
                   creditcard: true,
                   required: true,
                   minlength: 13,
                   maxlength: 19
              }
});

You can combine the credit card rule with the minimum and maximum length rules to achieve what you want. That may seem like too much effort -- and I might agree -- though it does give you more control if you only want to accept certain card number lengths.

$('form').validate({
    '#ccNum': {
                   creditcard: true,
                   required: true,
                   minlength: 13,
                   maxlength: 19
              }
});
烧了回忆取暖 2024-08-21 11:05:28

信用卡号码的长度可能会因发卡机构的不同而有所不同(不过,一般情况下,最小长度约为 13 位数字)。然而,由于这是客户端验证,因此重点可能更多地放在减少小拼写错误的可能性上,而不是检查任意数据。这可能应该在服务器端完成。

The length of credit card numbers can vary depending on the issuer (though yes, there's generally a minimum length of ~13 digits). However, since this is client-side validation, the focus is probably more on reducing the chance of a small typo, and less on checking arbitrary data. That should probably be done server-side.

半葬歌 2024-08-21 11:05:28

我已经更正了 jQuery 验证函数,以在验证中考虑信用卡长度。

我将以下内容添加到信用卡验证代码中 -

if (value.length > 19 || value.length<12)
{
    return (false);
}

信用卡验证部分的完整代码如下: -

creditcard: function(value, element) {
if ( this.optional(element) )
return "dependency-mismatch";
// accept only digits and dashes
if (/[^0-9-]+/.test(value))
return false;

// Modified part to check minimum and maximum card length
if (value.length > 19 || value.length<12)
{
    return (false);
}

var nCheck = 0,
nDigit = 0,
bEven = false;
value = value.replace(/\D/g, "");

for (n = value.length - 1; n >= 0; n--) {
var cDigit = value.charAt(n);
var nDigit = parseInt(cDigit, 10);
if (bEven) {
if ((nDigit *= 2) > 9)
nDigit -= 9;
}
nCheck += nDigit;
bEven = !bEven;
}

return (nCheck % 10) == 0;
},

我已将最小和最大卡长度分别硬编码为 12 和 19。

I have corrected the jQuery validate function to consider credit card length in validation.

I added the following to the credit card validation code-

if (value.length > 19 || value.length<12)
{
    return (false);
}

The complete code for the creditcard validation part is as follows:-

creditcard: function(value, element) {
if ( this.optional(element) )
return "dependency-mismatch";
// accept only digits and dashes
if (/[^0-9-]+/.test(value))
return false;

// Modified part to check minimum and maximum card length
if (value.length > 19 || value.length<12)
{
    return (false);
}

var nCheck = 0,
nDigit = 0,
bEven = false;
value = value.replace(/\D/g, "");

for (n = value.length - 1; n >= 0; n--) {
var cDigit = value.charAt(n);
var nDigit = parseInt(cDigit, 10);
if (bEven) {
if ((nDigit *= 2) > 9)
nDigit -= 9;
}
nCheck += nDigit;
bEven = !bEven;
}

return (nCheck % 10) == 0;
},

I have hard coded the minimum and maximum card lengths as 12 and 19 respectively.

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