用 javascript 计算欧洲 iban
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17197226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calculate european iban with javascript
提问by rubo77
I have the german "Bankleitzahl" and the account number, now I would like to calculate the corresponding IBAN with javascript, so I don't have to enter my banking-data on an untrustworthy website.
我有德语“Bankleitzahl”和帐号,现在我想用javascript计算相应的IBAN,所以我不必在不可信的网站上输入我的银行数据。
How would I calculate this with javascript on my own client?
我将如何在我自己的客户端上使用 javascript 计算这个?
回答by PhoneixS
I have found a project in github https://github.com/arhs/iban.jsthat make checking of IBAN a very easy stuff.
我在 github https://github.com/arhs/iban.js中找到了一个项目,它使检查 IBAN 变得非常简单。
Is something like:
是这样的:
<script src="iban.js"></script>
<script>
// the API is now accessible from the window.IBAN global object
IBAN.isValid('hello world'); // false
IBAN.isValid('BE68539007547034'); // true
</script>
Is licensed under the MIT license so you can check how it do the validation and try to use it to generate your IBAN.
在 MIT 许可下获得许可,因此您可以检查它如何进行验证并尝试使用它来生成您的 IBAN。
回答by fvu
So, if you want to reconstruct the IBAN code using the different old-style national components at hand, you can use the Wikipedia articleas a base. Specifically, a german IBAN is built up as follows:
因此,如果您想使用手头的不同旧式国家组件重建 IBAN 代码,您可以使用维基百科文章作为基础。具体来说,德国IBAN的构建如下:
DEkk bbbb bbbb cccc cccc cc
where
在哪里
- kk is the check code
- bbbbbbbb is the BLZ
- cccccccccc is the account number
- kk 是校验码
- bbbbbbbb 是 BLZ
- cccccccccc 是账号
Note: the exact format is country-dependent, the example above is for Germany
注意:确切的格式取决于国家/地区,上面的示例适用于德国
Tom's Cafeoffers a full JavasScript implementationyou can use in your own application, because the check sum calculation is not trivial.
Tom's Cafe提供了一个完整的 JavaScript 实现,您可以在您自己的应用程序中使用它,因为校验和计算并不简单。
回答by MaVRoSCy
Based on the work of http://toms-cafe.de/iban/iban.jsI have developed my version of IBAN check.
基于http://toms-cafe.de/iban/iban.js的工作,我开发了我的 IBAN 检查版本。
You can modify the country support by modifying the variable CODE_LENGTHS
可以通过修改变量CODE_LENGTHS来修改国家支持
Here is my implementation:
这是我的实现:
function alertValidIBAN(iban) {
alert(isValidIBANNumber(iban));
}
/*
* Returns 1 if the IBAN is valid
* Returns 0 if the IBAN's length is not as should be (for CY the IBAN Should be 28 chars long starting with CY )
* Returns any other number (checksum) when the IBAN is invalid (check digits do not match)
*/
function isValidIBANNumber(input) {
var CODE_LENGTHS = {
AD: 24, AE: 23, AT: 20, AZ: 28, BA: 20, BE: 16, BG: 22, BH: 22, BR: 29,
CH: 21, CR: 21, CY: 28, CZ: 24, DE: 22, DK: 18, DO: 28, EE: 20, ES: 24,
FI: 18, FO: 18, FR: 27, GB: 22, GI: 23, GL: 18, GR: 27, GT: 28, HR: 21,
HU: 28, IE: 22, IL: 23, IS: 26, IT: 27, JO: 30, KW: 30, KZ: 20, LB: 28,
LI: 21, LT: 20, LU: 20, LV: 21, MC: 27, MD: 24, ME: 22, MK: 19, MR: 27,
MT: 31, MU: 30, NL: 18, NO: 15, PK: 24, PL: 28, PS: 29, PT: 25, QA: 29,
RO: 24, RS: 22, SA: 24, SE: 24, SI: 19, SK: 24, SM: 27, TN: 24, TR: 26
};
var iban = String(input).toUpperCase().replace(/[^A-Z0-9]/g, ''), // keep only alphanumeric characters
code = iban.match(/^([A-Z]{2})(\d{2})([A-Z\d]+)$/), // match and capture (1) the country code, (2) the check digits, and (3) the rest
digits;
// check syntax and length
if (!code || iban.length !== CODE_LENGTHS[code[1]]) {
return false;
}
// rearrange country code and check digits, and convert chars to ints
digits = (code[3] + code[1] + code[2]).replace(/[A-Z]/g, function (letter) {
return letter.charCodeAt(0) - 55;
});
// final check
return mod97(digits);
}
function mod97(string) {
var checksum = string.slice(0, 2), fragment;
for (var offset = 2; offset < string.length; offset += 7) {
fragment = String(checksum) + string.substring(offset, offset + 7);
checksum = parseInt(fragment, 10) % 97;
}
return checksum;
}
Here is also a working fiddle
这也是一个工作小提琴
回答by ebram khalil
i think you need first to know or understand how it calculate it. i mean know what meth equation to do the calculations, however quick search on Google give me that:
我认为您首先需要知道或了解它是如何计算的。我的意思是知道用什么方法进行计算,但是在谷歌上快速搜索给了我:
i know they are not javascript as you want but you can check them and try to figure it's calculation(they are open source and free) and maybe then you can do your own JS library for it and later people ask you to help them
我知道它们不是你想要的 javascript,但你可以检查它们并尝试计算它(它们是开源和免费的),也许然后你可以为它做你自己的 JS 库,后来人们要求你帮助他们
IBAN Verification in C#, Excel Automation Add-in, Word SmartTag
回答by Srinivasan D
Try this,
试试这个,
https://stackoverflow.com/questions/21735674/iban-validation-jquery
https://stackoverflow.com/questions/21735674/iban-validation-jquery
This will evaluate the IBAN at client side..
这将在客户端评估 IBAN。
for jsfiddle output: