Validate IBANs

This is how you validate an IBAN using the IbanUtils class. Optionally you can restrict to CH and LI IBANs.

import ch.codeblock.qrinvoice.util.IbanUtils;

// ...

// When you use IbanUtils in order to validate IBAN numbers, you need to decide, whether the country code should be validated
// this is done using the 2nd parameter named "validateCountryCode"
// if set to true, the country code should be validated. If so, only "CH" and "LI" IBANs are allowed
// (the only supported IBANs are CH and LI for the QR Bill)
// if set to false, IBANs are validated without restriction on the country code

// you can validate IBANs in normalized format
System.out.println(IbanUtils.isValidIBAN("CH3908704016075473007", true));
// prints "true"

// or in formatted format, both is supported
System.out.println(IbanUtils.isValidIBAN("CH39 0870 4016 0754 7300 7", true));
System.out.println(IbanUtils.isValidIBAN("LI21 0881 0000 2324 013A A", true));
// prints "true"

// the following two lines show how to validate IBAN from Germany, Belgium and Malta
System.out.println(IbanUtils.isValidIBAN("DE89 3704 0044 0532 0130 00", false));
System.out.println(IbanUtils.isValidIBAN("BE68844010370034", false));
System.out.println(IbanUtils.isValidIBAN("MT98MMEB44093000000009027293051", false));
// prints "true"

Validate QR-IBANs

QR-IBAN validation is a specialization of IBAN validation, as it implicitly restricts country codes to CH and LI and checks, that the IID is withing the specified QR-IID range of 30000 – 31999.

import ch.codeblock.qrinvoice.util.IbanUtils;

// ...

System.out.println(IbanUtils.isQrIBAN("CH44 3199 9123 0008 8901 2"));
// prints "true"

System.out.println(IbanUtils.isQrIBAN("CH39 0870 4016 0754 7300 7"));
// prints "false"