信用卡的客户端验证

发布于 2024-07-08 09:51:29 字数 53 浏览 12 评论 0原文

是否有人有一个库或 JavaScript 片段可以在用户点击“提交”之前验证信用卡的校验位?

Does anyone have a library or JavaScript snippet to validate the check digit of credit cards before the user hits Submit?

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

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

发布评论

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

评论(6

晚雾 2024-07-15 09:51:29

The jQuery Validation Plugin has a method for validating credit card numbers.

There are other specific scripts:

Most of them use the Luhn algorithm.

迷迭香的记忆 2024-07-15 09:51:29

也许OP甚至不再关注这个线程,但这可能对其他人有帮助:

http://jquerycreditcardvalidator.com

它检查卡类型,验证其长度并使用 Luhn 算法检查 mod 10。

Probably OP doesn't even follow this thread anymore but this may be helpful for someone else:

http://jquerycreditcardvalidator.com

It checks the card type, validates its length and checks for mod 10 with Luhn algorithm.

锦欢 2024-07-15 09:51:29

您可以使用此代码段通过 Luhn 算法 验证 16 位卡号:

function validateCardNumber(number) {
    var regex = new RegExp("^[0-9]{16}$");
    if (!regex.test(number))
        return false;

    return luhnCheck(number);
}

function luhnCheck(val) {
    var sum = 0;
    for (var i = 0; i < val.length; i++) {
        var intVal = parseInt(val.substr(i, 1));
        if (i % 2 == 0) {
            intVal *= 2;
            if (intVal > 9) {
                intVal = 1 + (intVal % 10);
            }
        }
        sum += intVal;
    }
    return (sum % 10) == 0;
}

You can use this snippet to validate 16 digits card numbers with Luhn algorithm:

function validateCardNumber(number) {
    var regex = new RegExp("^[0-9]{16}$");
    if (!regex.test(number))
        return false;

    return luhnCheck(number);
}

function luhnCheck(val) {
    var sum = 0;
    for (var i = 0; i < val.length; i++) {
        var intVal = parseInt(val.substr(i, 1));
        if (i % 2 == 0) {
            intVal *= 2;
            if (intVal > 9) {
                intVal = 1 + (intVal % 10);
            }
        }
        sum += intVal;
    }
    return (sum % 10) == 0;
}
ヤ经典坏疍 2024-07-15 09:51:29

Luhn 算法(也称为 Luhn 公式)可用于验证各种标识号(例如 < strong>信用卡号、IMEI)。

我省略了对该算法的解释,因为它已经被其他人暴露过,但如果你需要最快的Javascript实现,你可以看到它此处

简而言之...

function luhn(array) {
  return function (number) {
    let len = number ? number.length : 0,
      bit = 1,
      sum = 0;

    while (len--) {
      sum += !(bit ^= 1) ? parseInt(number[len], 10) : array[number[len]];
    }
    return sum % 10 === 0 && sum > 0;
  };
}([0, 2, 4, 6, 8, 1, 3, 5, 7, 9]);

请注意,链接的源代码采用 ES6 语言(也称为 JavaScript 2015),但以 ES5 进行转译(请参阅 index.js) 并且它经过了全面的单元测试。

此外,它可以在浏览器和/或node.js中使用。

基准测试和其他实现位于 jsperf 上,以验证其高性能。

现在,如果您只是想使用它,请从链接的存储库中获取代码。

否则通过 bower 安装它...

bower install luhn-alg

或者通过 npm ...

npm install luhn-alg

免责声明:我是 luhn-alg 包的作者。

Luhn algorithm (also known as Luhn formula) is useful to validate a variety of identification numbers (e.g. credit card numbers, IMEI).

I omit the explanation of the algorithm because it has already been exposed by others but if you need the fastest Javascript implementation, you can see it here.

Put simply ...

function luhn(array) {
  return function (number) {
    let len = number ? number.length : 0,
      bit = 1,
      sum = 0;

    while (len--) {
      sum += !(bit ^= 1) ? parseInt(number[len], 10) : array[number[len]];
    }
    return sum % 10 === 0 && sum > 0;
  };
}([0, 2, 4, 6, 8, 1, 3, 5, 7, 9]);

Note that linked source is in ES6 language (also known as JavaScript 2015), but is transpiled in ES5 (see index.js) and it is fully unit tested.

Furthermore it is usable both in browsers and/or node.js.

Benchmarks and other implementation are on jsperf to verify its high performances.

Now, if you simply want to use it grab the code from the linked repository.

Otherwise install it via bower ...

bower install luhn-alg

Or via npm ...

npm install luhn-alg

Disclaimer: I am the author of the luhn-alg package.

箜明 2024-07-15 09:51:29

如果您尚未使用 jQuery 插件,则可以使用此功能。 它基于 Luhn 算法,可以容忍空格或破折号,因此应该适用于您需要它的大多数数据输入情况。

http://af-design.com/blog /2010/08/18/验证信用卡号码/

You can use this function if you're not already using the jQuery plugin. It's based on the Luhn algorithm and is tolerant of spaces or dashes so should work for most data entry cases you would need it for.

http://af-design.com/blog/2010/08/18/validating-credit-card-numbers/

倾城°AllureLove 2024-07-15 09:51:29

Luhn公式是信用卡验证中最流行的算法。 并且不要太害怕“算法”这个词,以至于您正在寻找一个库。 这非常容易理解。 从维基百科的描述来看,该算法可以分为3步:

  • 从最右边的数字(即校验位)开始,向左移动,每隔一个数字的值加倍; 如果这个产品
    加倍运算大于 9(例如,8 × 2 = 16),然后求和
    乘积的位数(例如 16: 1 + 6 = 7、18: 1 + 8 = 9)。
  • 计算所有数字的总和。
  • 如果总和模 10 等于 0(如果总和以零结尾),则根据 Luhn 公式,该数字有效; 否则它不是
    有效。

这是我的工作草稿。

function luhn(anum){
    anum = anum+'';
    var sum = 0,
        max = anum.length - 1;
    //From the rightmost digit, which is the check digit, moving left
    for(var j=max;j>=0;j--){
        var digit = parseInt(anum[j]);
        //Take the sum of all the digits
        if((max - j) & 1){
            //double the value of every second digit
            var add = digit * 2;
            //if the product of this doubling operation is greater than 9 ,
            //then sum the digits of the products
            sum += add < 10 ? add : 1 + add % 10;
        }else{
            sum += digit;
        }
    }
    //If the total modulo 10 is equal to 0 (if the total ends in zero)
    //then the number is valid according to the Luhn formula;else it is not valid.
    return sum % 10 === 0;
}

luhn(79927398713) -> true

Luhn formula is the most popular algorithm in credit card validation. And don't be so afraid of the word algorithm that you're looking for a library. It's incredibly easy to understand. From Wikipedia description, this algorithm can be divide in 3 steps:

  • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this
    doubling operation is greater than 9 (e.g., 8 × 2 = 16), then sum the
    digits of the products (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9).
  • Take the sum of all the digits.
  • If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not
    valid.

Here is my working draft.

function luhn(anum){
    anum = anum+'';
    var sum = 0,
        max = anum.length - 1;
    //From the rightmost digit, which is the check digit, moving left
    for(var j=max;j>=0;j--){
        var digit = parseInt(anum[j]);
        //Take the sum of all the digits
        if((max - j) & 1){
            //double the value of every second digit
            var add = digit * 2;
            //if the product of this doubling operation is greater than 9 ,
            //then sum the digits of the products
            sum += add < 10 ? add : 1 + add % 10;
        }else{
            sum += digit;
        }
    }
    //If the total modulo 10 is equal to 0 (if the total ends in zero)
    //then the number is valid according to the Luhn formula;else it is not valid.
    return sum % 10 === 0;
}

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