【问题标题】:How to check IBAN validation?如何检查 IBAN 验证?
【发布时间】:2019-07-19 23:05:51
【问题描述】:

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

国际银行帐号是一种国际公认的跨国界银行账户识别系统,以促进跨境交易的沟通和处理,同时降低转录错误的风险。

【问题讨论】:

    标签: java android banking bank iban


    【解决方案1】:
    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;
    }
    

    【讨论】:

      【解决方案2】:

      实施这些步骤:https://en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN

      另外,long 太小,无法容纳数字。你需要一个 BigInteger。

      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);
      }
      

      【讨论】:

        猜你喜欢
        • 2014-03-22
        • 2015-05-30
        • 2018-10-17
        • 2019-04-06
        • 2014-01-25
        • 2015-11-19
        • 2017-10-03
        • 2018-11-30
        • 2016-11-08
        相关资源
        最近更新 更多