首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何检查IBAN验证?

如何检查IBAN验证?
EN

Stack Overflow用户
提问于 2019-02-26 11:52:18
回答 2查看 4.8K关注 0票数 1

如何在java中验证IBAN (国际银行帐号)在Android应用程序中的使用?

国际银行帐号是一种国际商定的跨国界银行账户识别系统,以便利跨境交易的沟通和处理,减少抄写错误的风险。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-26 11:52:18

代码语言:javascript
运行
复制
private boolean isIbanValid(String iban) {

    int IBAN_MIN_SIZE = 15;
    int IBAN_MAX_SIZE = 34;
    long IBAN_MAX = 999999999;
    long IBAN_MODULUS = 97;

    String trimmed = iban.trim();

    if (trimmed.length() < IBAN_MIN_SIZE || trimmed.length() > IBAN_MAX_SIZE) {
        return false;
    }

    String reformat = trimmed.substring(4) + trimmed.substring(0, 4);
    long total = 0;

    for (int i = 0; i < reformat.length(); i++) {

        int charValue = Character.getNumericValue(reformat.charAt(i));

        if (charValue < 0 || charValue > 35) {
            return false;
        }

        total = (charValue > 9 ? total * 100 : total * 10) + charValue;

        if (total > IBAN_MAX) {
            total = (total % IBAN_MODULUS);
        }
    }

    return (total % IBAN_MODULUS) == 1;
}
票数 4
EN

Stack Overflow用户

发布于 2020-09-23 10:18:33

实现这些步骤:IBAN

另外,一个长太小,容纳不了这个数字。你需要一个BigInteger。

代码语言:javascript
运行
复制
private boolean isIbanValid(String iban) {
    // remove spaces
    iban = iban.replace("\\s", "");

    // make all uppercase
    iban = iban.toUpperCase();

    // check length - complicated, depends on country. Skipping for now.
    // https://en.wikipedia.org/wiki/International_Bank_Account_Number#Structure

    // move first four letters to the end
    iban = iban.substring(4) + iban.substring(0,4);

    // convert letters to digits
    String total = "";
    for (int i = 0; i < iban.length(); i++) {

        int charValue = Character.getNumericValue(iban.charAt(i));

        if (charValue < 0 || charValue > 35) {
            return false;
        }

        total += charValue;
    }

    // Make BigInteger and check if modulus 97 is 1 
    BigInteger totalInt = new BigInteger(total);
    return totalInt.mod(new BigInteger("97")).equals(BigInteger.ONE);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54884930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档