This page covers lenient and strict parsing, the three output formats, an IBAN's parts, the MOD 97-10 check digit helpers and typo suggestions.
Lenient and strict parsing
Iban::parse (and verify) normalises the input first, as to_machine_format does:
- Inputs over 1024 bytes are rejected before any other work.
- ASCII letters are upper-cased, and leading spaces, tabs, line breaks, vertical tabs and NUL bytes are removed.
- One leading
IBANorIIBANtag is removed, with or without a separator after it. - Every remaining byte that is not
A–Zor0–9is dropped: spaces, dashes, dots, non-breaking spaces and any other non-ASCII character.
Iban::parse_strict (and verify_strict) skips normalisation. It accepts only the electronic format:
upper-case A–Z and 0–9, no separators, no tag. Use it for data that should already be clean,
such as a value read back from your database.
use laravel_rust_iban::{Error, Iban, to_machine_format, verify_strict};
fn main() -> Result<(), Error> {
assert_eq!(to_machine_format(" iban be68-5390-0754-7034\n")?, "BE68539007547034");
assert!(Iban::parse("be68 5390 0754 7034").is_ok());
assert_eq!(Iban::parse_strict("be68 5390 0754 7034"), Err(Error::NotMachineFormat));
assert!(verify_strict("BE68539007547034"));
Ok(())
}
to_machine_format normalises the text. Use Iban::parse to validate it.
Formats
| Format | Iban method | Free function | Example |
|---|---|---|---|
| Electronic | machine_format(), to_string() | to_machine_format | BE68539007547034 |
| Paper, groups of four | human_format() | to_human_format | BE68 5390 0754 7034 |
| Masked, for display | obfuscated_format() | to_obfuscated_format | BE** **** **** 7034 |
The masked form keeps the country code and the last four characters and replaces the rest with *.
Prefer the methods on a parsed Iban: they always produce a correct result. The free functions work on
raw strings for code that moves from PHP:
to_human_formatremoves ASCII spaces and groups what is left. Give it electronic text. Other characters, such as dashes, are kept as they are.to_obfuscated_formatnormalises its input first and returnsError::InvalidLengthwhen fewer than six characters remain.
use laravel_rust_iban::{to_human_format, to_obfuscated_format};
fn main() -> Result<(), laravel_rust_iban::Error> {
assert_eq!(to_human_format("NO9386011117947")?, "NO93 8601 1117 947");
assert_eq!(to_obfuscated_format("be68 5390 0754 7034")?, "BE** **** **** 7034");
Ok(())
}Parts
A parsed Iban gives each part as a borrowed &str. The bank, branch and national check digit
positions come from the country's registry entry. Missing parts return None.
use laravel_rust_iban::Iban;
fn main() -> Result<(), laravel_rust_iban::Error> {
let iban = Iban::parse("BE68539007547034")?;
assert_eq!(iban.country_code(), "BE");
assert_eq!(iban.check_digits(), "68");
assert_eq!(iban.bban(), "539007547034");
assert_eq!(iban.bank(), Some("539"));
assert_eq!(iban.branch(), None);
assert_eq!(iban.account(), Some("007547034"));
assert_eq!(iban.national_check_digits(), Some("34"));
Ok(())
}
The account part runs from the end of the branch identifier (or of the bank identifier, when there is no branch identifier) to the end of the BBAN, so it includes trailing national check digits.
Iban::parts() returns all seven at once as an IbanParts<&str>, with the fields country,
check_digits, bban, bank, branch, account and national_check_digits.
The free function parts returns an IbanParts<String> without checking the check digits. It still
needs 5 to 34 characters and two leading letters, and for an unregistered prefix the country-specific
parts are None:
fn main() -> Result<(), laravel_rust_iban::Error> {
let parts = laravel_rust_iban::parts("be00 5390 0754 7034")?;
assert_eq!(parts.check_digits, "00");
assert_eq!(parts.bank.as_deref(), Some("539"));
Ok(())
}International check digits
These helpers implement ISO 13616 check digits with ISO/IEC 7064 MOD 97-10. They check the header and
the arithmetic. Use verify to check the country, length and BBAN structure.
| Function | Returns |
|---|---|
verify_checksum(input) | true when the check digits are 02 to 98 and the MOD 97-10 remainder is 1 |
find_checksum(input) | The correct two check digits, ignoring the ones in the input |
set_checksum(input) | The electronic IBAN with its check digits recalculated |
checksum_string_replace(input) | The input with each upper-case letter replaced by its value, A = 10 to Z = 35 |
mod97_10(digits) | true when the remainder of the digit string modulo 97 is 1 |
mod97_10_checksum(digits) | The remainder modulo 97, as a u8, for 1 to 1024 decimal digits |
use laravel_rust_iban::{find_checksum, mod97_10_checksum, set_checksum, verify_checksum};
fn main() -> Result<(), laravel_rust_iban::Error> {
assert!(verify_checksum("BE68539007547034"));
assert_eq!(find_checksum("BE00539007547034")?, "68");
assert_eq!(set_checksum("be99 5390 0754 7034")?, "BE68539007547034");
assert_eq!(mod97_10_checksum("539007547034111400")?, 30);
Ok(())
}
Generated check digits are always in the range 02 to 98. The values 00, 01 and 99 never
result from the calculation (ISO 13616-1:2020 §6.3.3), so every check rejects them, even when the
remainder happens to be 1.
mod97_10_checksum returns Error::InvalidLength for an empty string, Error::InvalidStructure for a
non-digit and Error::InputTooLong above 1024 bytes. It works digit by digit, so long numbers need no
big-integer support.
Typo suggestions
mistranscription_suggestions helps when someone copied an IBAN from paper or a scan and it fails
validation. It tries the characters that are easy to misread for each other (0, O, D and Q;
1, I and L; 2 and Z; 5 and S; 8 and B; 6 and G) and returns every variant that is
a valid IBAN, in electronic form:
use laravel_rust_iban::mistranscription_suggestions;
fn main() {
assert_eq!(mistranscription_suggestions("BE68539O07547034"), ["BE68539007547034"]);
assert_eq!(mistranscription_suggestions("BE68 539O O754 7O34"), ["BE68539007547034"]);
}
It first replaces one character at a time (only letters in the country code, only digits in the check digits), then replaces every occurrence of a repeated character at once. The list has no duplicates and is empty when nothing matches or the input is shorter than 5 or longer than 34 characters. Show the suggestions so the user can choose the intended account.