IBAN验证校验和

发布于 2025-02-09 12:48:25 字数 99 浏览 1 评论 0原文

如何获取一串Iban号码。我需要计算支票数字,但是字符太多。

这是一个示例iban:br 0012345678901234567890123

How to get a string of an IBAN number. I need to calculate the check digit, but there are too many characters.

This is an example IBAN: BR0012345678901234567890123

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

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

发布评论

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

评论(1

晨曦÷微暖 2025-02-16 12:48:25

“ noreferrer”>“验证i ban” 。如所述,只需遵循算法即可。

请按照下面的代码查看如何逐步进行验证。

注意: 计算模量需要。

const validate = (iban) => {
  const
    [, head, tail] = iban.split(/(^\w{4})(\w+)$/),
    rearrange = `${tail}${head}`,
    replace = rearrange
      .split('')
      .map(c =>
        /[a-z]/i.test(c)
          ? c.toLowerCase().charCodeAt(0) - 87
          : parseInt(c, 10)
        )
        .join('') 
  return BigInt(replace) % 97n === 1n;
};

// This IBAN is invalid, since the remainder is not 1
console.log(validate('BR0012345678901234567890123'));

// Valid, via https://www.iban.com/iban-checker example
console.log(validate('GB33BUKB20201555555555'));

可以简单地简单地缩短:

const validate = (iban) =>
  BigInt([...iban.slice(4), ...iban.slice(0, 4)]
    .map(c => /[a-z]/i.test(c) ? c.toLowerCase().charCodeAt(0) - 87 : c)
    .join('')) % 97n === 1n;

// This IBAN is invalid, since the remainder is not 1
console.log(validate('BR0012345678901234567890123'));

// Valid, via https://www.iban.com/iban-checker example
console.log(validate('GB33BUKB20201555555555'));

IBAN validation is described on the Wikipedia page under "Validating the IBAN". Just follow the algorithm as described.

Follow the code below to see how validation happens step-by-step.

Note: BigInt is required to calculate the modulo.

const validate = (iban) => {
  const
    [, head, tail] = iban.split(/(^\w{4})(\w+)$/),
    rearrange = `${tail}${head}`,
    replace = rearrange
      .split('')
      .map(c =>
        /[a-z]/i.test(c)
          ? c.toLowerCase().charCodeAt(0) - 87
          : parseInt(c, 10)
        )
        .join('') 
  return BigInt(replace) % 97n === 1n;
};

// This IBAN is invalid, since the remainder is not 1
console.log(validate('BR0012345678901234567890123'));

// Valid, via https://www.iban.com/iban-checker example
console.log(validate('GB33BUKB20201555555555'));

This can be shortened to simply:

const validate = (iban) =>
  BigInt([...iban.slice(4), ...iban.slice(0, 4)]
    .map(c => /[a-z]/i.test(c) ? c.toLowerCase().charCodeAt(0) - 87 : c)
    .join('')) % 97n === 1n;

// This IBAN is invalid, since the remainder is not 1
console.log(validate('BR0012345678901234567890123'));

// Valid, via https://www.iban.com/iban-checker example
console.log(validate('GB33BUKB20201555555555'));

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