isShebaValid static method

bool isShebaValid(
  1. String sheba
)

Checks an IBAN/Sheba "IR" + 24 digits (mod 97). UPersianTools.isShebaValid("IR062960000000100324200001")

Implementation

static bool isShebaValid(String sheba) {
  if (sheba.length != 26 || !RegExp("IR[0-9]{24}").hasMatch(sheba)) return false;
  final int d1 = sheba.codeUnitAt(0) - 65 + 10;
  final int d2 = sheba.codeUnitAt(1) - 65 + 10;
  final String iban = "${sheba.substring(4)}$d1$d2${sheba.substring(2, 4)}";
  String remainder = iban;
  while (remainder.length > 2) {
    final String block = remainder.length >= 9 ? remainder.substring(0, 9) : remainder;
    remainder = "${int.parse(block) % 97}${remainder.substring(block.length)}";
  }
  return int.parse(remainder) % 97 == 1;
}