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
python
import re from collections import Counter from pathlib import Path
Ranking the top words in a PDF
text-processing
regex
counting
Intermediate
6 steps
java
@Configuration @EnableWebSecurity public class OAuth2SecurityConfig {
How OIDC login works in Spring Security
oauth2
openid-connect
authorization
Intermediate
8 steps
go
package config import ( "fmt"
Loading and saving YAML config in Go
yaml
struct-tags
defaults
Intermediate
8 steps
java
@RestController @RequestMapping("/api/reports") public class OrderReportController {
Streaming a CSV export in Spring
streaming
csv-export
backpressure
Intermediate
9 steps
java
public class SlidingLogRateLimiter { private final int maxRequests; private final long windowMillis;
How a sliding-log rate limiter works
rate-limiting
concurrency
sliding-window
Advanced
8 steps
php
<?php namespace App\Http\Controllers;
Streaming a filtered CSV export in Laravel
streaming
csv-export
query-builder
Intermediate
9 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.