java
43 lines · 8 steps
Validating IBANs with the mod-97 checksum
A stateless validator that checks IBAN structure, country length, and the ISO 7064 mod-97 checksum.
Explained by
highlit
1public final class IbanValidator {
2
3 private static final Map<String, Integer> COUNTRY_LENGTHS = Map.ofEntries(
4 Map.entry("DE", 22), Map.entry("FR", 27), Map.entry("GB", 22),
5 Map.entry("NL", 18), Map.entry("ES", 24), Map.entry("IT", 27),
6 Map.entry("BE", 16), Map.entry("CH", 21), Map.entry("AT", 20)
7 );
8
9 private IbanValidator() {
10 }
11
12 public static boolean isValid(String iban) {
13 if (iban == null) {
14 return false;
15 }
16
17 String normalized = iban.replace(" ", "").toUpperCase();
18 if (!normalized.matches("[A-Z]{2}\\d{2}[A-Z0-9]+")) {
19 return false;
20 }
21
22 String country = normalized.substring(0, 2);
23 Integer expectedLength = COUNTRY_LENGTHS.get(country);
24 if (expectedLength == null || normalized.length() != expectedLength) {
25 return false;
26 }
27
28 String rearranged = normalized.substring(4) + normalized.substring(0, 4);
29 return computeMod97(rearranged) == 1;
30 }
31
32 private static int computeMod97(String rearranged) {
33 int remainder = 0;
34 for (int i = 0; i < rearranged.length(); i++) {
35 char c = rearranged.charAt(i);
36 int value = Character.isLetter(c) ? c - 'A' + 10 : c - '0';
37 remainder = value > 9
38 ? (remainder * 100 + value) % 97
39 : (remainder * 10 + value) % 97;
40 }
41 return remainder;
42 }
43}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Layering cheap structural checks before expensive computation lets you reject bad input fast.
- 2The mod-97 algorithm avoids huge integers by folding the remainder digit-by-digit as it scans.
- 3A private constructor plus static methods signals a pure, stateless utility class.
Related explainers
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/validating-ibans-with-the-mod-97-checksum-explained-java-9272/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.