public final class Validations {
static Integer CCI_LENGTH_ST = 18;
static Integer CCI_LENGTH_FST = 20;
static String EMPTY_CHECK_DIGITS = "00";
public static boolean validateBankAccount(String bankAccount) {
if (!ValidationsUtils.validateOnlyNumbers(bankAccount)) {
int accountLength = bankAccount.length();
String lastDigits = bankAccount.substring(bankAccount.length() - 2);
if (accountLength == CCI_LENGTH_ST && lastDigits.equals(EMPTY_CHECK_DIGITS)) {
return validateCCI(bankAccount);
//Validate CCI bank account
public static boolean validateCCI(String cci) {
if (validateCCILength(cci)) {
String cciWithoutCheck = cci.substring(0, cci.length() - 2);
String checkDigits = cci.substring(cci.length() - 2);
String calculatedCheckDigits = getCciCheckDigits(cciWithoutCheck);
return checkDigits.equals(calculatedCheckDigits);
public static boolean validateCCILength(String cci) {
return cci.length() == CCI_LENGTH_FST;
public static String getCciCheckDigits(String cci) {
int firstControlNumber = calculateCheckDigit(cci.substring(0, 6));
int secondControlNumber = calculateCheckDigit(cci.substring(6, 18));
return String.valueOf(firstControlNumber) + String.valueOf(secondControlNumber);
private static int calculateCheckDigit(String cci) {
for (int i = 0; i < cci.length(); i++) {
String[] cciArray = cci.split("");
int num = Integer.parseInt(cciArray[i]);
int product = (num * factor);
String product_str = Integer.toString(product);
int firstDigit = Integer.parseInt(product_str.substring(0, 1));
int lastDigit = product % 10;
total += firstDigit + lastDigit;
factor = factor == 1 ? 2 : 1;
return (total % 10) > 0 ? 10 - (total % 10) : 0;