42 是有效的信用卡号码吗? jQuery 验证器认为是
我们都知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能是因为此验证器仅检查提供的数字是否满足 LUHN-10< /a> 算法(42 满足,因为 4*2 + 2 = 10,即 0 模 10)。
更好的验证器可能应该检查最少数量的数字。
我不确定这是否对应于 jQuery 的最新代码,但我找到了与信用卡验证相关的代码片段:
...正如您所看到的,这只是检查所有字符是否都是数字并且 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:
... 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.
您可以将信用卡规则与最小和最大长度规则结合起来以实现您想要的。这可能看起来需要付出太多的努力——我可能同意——尽管如果您只想接受某些卡号长度,它确实会给您更多的控制权。
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.
信用卡号码的长度可能会因发卡机构的不同而有所不同(不过,一般情况下,最小长度约为 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.
我已经更正了 jQuery 验证函数,以在验证中考虑信用卡长度。
我将以下内容添加到信用卡验证代码中 -
信用卡验证部分的完整代码如下: -
我已将最小和最大卡长度分别硬编码为 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-
The complete code for the creditcard validation part is as follows:-
I have hard coded the minimum and maximum card lengths as 12 and 19 respectively.