validateIBAN function

ValidateIBANResult validateIBAN(
  1. String? iban, {
  2. ValidateIBANOptions validationOptions = const ValidateIBANOptions(),
})

validateIBAN

// `returns ValidateIBANResult(errorCodes: <ValidationErrorsIBAN>[], valid: true)`
ibantools.validateIBAN("NL91ABNA0417164300");
```dart
// `returns ValidateIBANResult(errorCodes: <ValidationErrorsIBAN>[], valid: true)`
ibantools.validateIBAN('CH4431999123000889012');
// returns `ValidateIBANResult(errorCodes: <ValidationErrorsIBAN>[ValidationErrorsIBAN.wrongAccountBankBranchChecksum], valid: false)`
ibantools.validateIBAN('CH4431999123000889012', { allowQRIBAN: false });

Implementation

ValidateIBANResult validateIBAN(String? iban,
    {ValidateIBANOptions validationOptions = const ValidateIBANOptions()}) {
  ValidateIBANResult result = const ValidateIBANResult(
      errorCodes: <ValidationErrorsIBAN>[], valid: true);
  if (iban != null && iban.isNotEmpty) {
    final CountrySpec? spec = countrySpecs[iban.substring(0, 2)];
    if (spec == null || !(spec.bbanRegexp != null || spec.chars != null)) {
      result = result.copyWith(
          valid: false,
          errorCodes: List<ValidationErrorsIBAN>.of(
              result.errorCodes..add(ValidationErrorsIBAN.noIBANCountry)));
      return result;
    }

    if (spec.chars != null && spec.chars != iban.length) {
      result = result.copyWith(
          valid: false,
          errorCodes: List<ValidationErrorsIBAN>.of(
              result.errorCodes..add(ValidationErrorsIBAN.wrongBBANLength)));
    }

    if (spec.bbanRegexp != null &&
        !_checkFormatBBAN(iban.substring(4), spec.bbanRegexp!)) {
      result = result.copyWith(
          valid: false,
          errorCodes: List<ValidationErrorsIBAN>.of(
              result.errorCodes..add(ValidationErrorsIBAN.wrongBBANFormat)));
    }

    if (spec.bbanValidationFunc != null &&
        !spec.bbanValidationFunc!(iban.substring(4))) {
      result = result.copyWith(
          valid: false,
          errorCodes: List<ValidationErrorsIBAN>.of(result.errorCodes
            ..add(ValidationErrorsIBAN.wrongAccountBankBranchChecksum)));
    }

    final RegExp reg = RegExp(r'^[0-9]{2}$');
    if (!reg.hasMatch(iban.substring(2, 4))) {
      result = result.copyWith(
          valid: false,
          errorCodes: List<ValidationErrorsIBAN>.of(
              result.errorCodes..add(ValidationErrorsIBAN.checksumNotNumber)));
    }

    if (result.errorCodes.contains(ValidationErrorsIBAN.wrongBBANFormat) ||
        !isValidIBANChecksum(iban)) {
      result = result.copyWith(
          valid: false,
          errorCodes: List<ValidationErrorsIBAN>.of(
              result.errorCodes..add(ValidationErrorsIBAN.wrongIBANChecksum)));
    }

    if (!validationOptions.allowQRIBAN && isQRIBAN(iban)) {
      result = result.copyWith(
          valid: false,
          errorCodes: List<ValidationErrorsIBAN>.of(
              result.errorCodes..add(ValidationErrorsIBAN.qRIBANNotAllowed)));
    }
  } else {
    result = result.copyWith(
        valid: false,
        errorCodes: List<ValidationErrorsIBAN>.of(
            result.errorCodes..add(ValidationErrorsIBAN.noIBANProvided)));
  }

  return result;
}