java - 如何检查 IBAN 验证?
问题描述
如何在 Java 中验证 IBAN(国际银行帐号)以在 Android 应用程序中使用?
国际银行帐号是一种国际公认的识别跨国银行账户的系统,以促进跨境交易的沟通和处理,同时降低转录错误的风险。
解决方案
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;
}
推荐阅读
- php - 特定图像文件破坏格式,拒绝发布到 PHP
- python - 如何根据字符串合并具有不同长度的两个数据帧包含
- internet-explorer - IE 从 URL 中删除参数
- r - 使用 paste0 通过 for 循环创建多个对象名称
- django - ImportError:没有命名模块。Worker 无法启动
- android - Kotlin:如何以共享首选项删除存储在数组中的项目
- react-native - 如何使用 react-native 将数据保存在 Salesforce 帐户中?
- twitter-bootstrap - 使用 ng add 命令安装 ngx-bootstrap
- javascript - 总结 Javascript 中的替换函数
- r - install.packages() 强制不选择 CRAN 镜像?