Home

Awesome

IBAN Validator

IBAN Validator for your web application forms (Angular, React, Vue, ...), comes without any dependencies and can be used as a standalone function in any JS project. It can perform format, digit and length IBAN validations. Currently 112 countries are supported.

Content

Install

npm install ngx-iban-validator --save

Use as a standalone function

You can use validateIBAN function independently from any forms.

Value can be passed as part of object in this case validation flow will be the same as for form validation:

const ibanIsInvalid =
  validateIBAN({
    value: "AL35202111090000000001234567",
  }) !== null;

Value can be passed as a string:

const ibanIsInvalid = validateIBAN("AL35202111090000000001234567").ibanInvalid;

NodeJS

const ibanValidator = require("ngx-iban-validator");
const ibanIsInvalid = ibanValidator.validateIBAN(
  "BA393385804800211234"
).ibanInvalid;

Error Response

export interface IBANValidationResult {
  ibanInvalid: boolean;
  error: IBANError;
}

export interface IBANError {
  countryUnsupported: boolean;
  codeLengthInvalid: boolean;
  patternInvalid: boolean;
}

Error object contains more details about validation error. You can display errors easily as with any other validator.

Use as a form validator

Angular

Import validateIBAN function from ngx-iban-validator package into your component file. Add validateIBAN to your form validators array.

import { Component, inject } from "@angular/core";
import { NgIf } from "@angular/common";
import {
  FormBuilder,
  FormGroup,
  ReactiveFormsModule,
  Validators,
} from "@angular/forms";

import { validateIBAN } from "ngx-iban-validator";

@Component({
  selector: "my-app",
  standalone: true,
  imports: [NgIf, ReactiveFormsModule],
  template: `
    <div class="page">
      <form [formGroup]="ibanForm" (ngSubmit)="submit(ibanForm)">
        <h2>NGX IBAN Validator</h2>
        <div>
          <input type="text" formControlName="iban" placeholder="Enter IBAN" />
          <button [disabled]="ibanForm.invalid">Submit</button>
        </div>
        <div class="validation-errors">
          <small
            *ngIf="
                ibanForm.get('iban')?.errors && ibanForm.get('iban')?.errors?.['ibanInvalid']
              "
          >
            <span
              *ngIf="ibanForm.get('iban')?.errors?.['error']['countryUnsupported']"
            >
              Country not supported
            </span>
            <span
              *ngIf="ibanForm.get('iban')?.errors?.['error']['codeLengthInvalid']"
            >
              IBAN Code length is invalid
            </span>
            <span
              *ngIf="ibanForm.get('iban')?.errors?.['error']['patternInvalid']"
            >
              IBAN Code pattern is invalid
            </span>
          </small>
        </div>
      </form>
    </div>
  `,
  styles: [
    `
      .page {
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      h2 {
        text-align: center;
      }
      form {
        padding: 20px;
      }
      input {
        padding: 10px;
      }
      button {
        padding: 10px;
      }
      .validation-errors {
        color: red;
      }
    `,
  ],
})
export class App {
  formBuilder = inject(FormBuilder);
  ibanForm = this.formBuilder.group({
    iban: [null, [Validators.required, validateIBAN]],
  });
  submit(form: FormGroup) {
    const { valid, value } = form;
    const { iban } = value;
    if (valid) {
      alert(`IBAN: ${iban}, is valid!`);
    }
  }
}

React

import { useState } from "react";
import {
  IBANError,
  IBANValidationResult,
  validateIBAN,
} from "ngx-iban-validator/dist/iban.validator";

import "./App.css";

function App() {
  const [error, setError] = useState<IBANError | null>(null);

  const validate = (iban: string): boolean => {
    const validation = validateIBAN({
      value: iban,
    });
    if (validation) {
      const { ibanInvalid, error }: IBANValidationResult = validation;
      if (ibanInvalid) {
        setError(error);
        return false;
      } else {
        setError(null);
        return true;
      }
    } else {
      setError(null);
      return true;
    }
  };

  const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    const formData = new FormData(event.currentTarget);
    const iban = formData.get("iban") as string;
    const validation = validate(iban);
    if (validation) {
      alert("IBAN is valid");
    } else {
      alert("IBAN is not valid");
    }
  };

  const handleIbanChanged = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { value } = event.target;
    validate(value);
  };

  return (
    <div className="page">
      <form onSubmit={handleSubmit}>
        <h2>NGX IBAN Validator</h2>
        <div>
          <input
            type="text"
            name="iban"
            placeholder="Enter IBAN"
            onChange={handleIbanChanged}
          />
          <button>Submit</button>
        </div>
        <div className="validation-errors">
          <small hidden={!error}>
            <span hidden={!error?.countryUnsupported}>
              Country not supported
            </span>
            <span hidden={!error?.codeLengthInvalid}>
              IBAN Code length is invalid
            </span>
            <span hidden={!error?.patternInvalid}>
              IBAN Code pattern is invalid
            </span>
          </small>
        </div>
      </form>
    </div>
  );
}

export default App;

Demo

Check demo applications for usage examples:

Supported countries

<details> <summary> Click here to expand list of supported countries </summary>
NoCountryCountry CodeLength
1AlbaniaAL28
2AlgeriaDZ26
3AndorraAD24
4AngolaAO25
5AustriaAT20
6AzerbaijanAZ28
7BahrainBH22
8BelarusBY28
9BelgiumBE16
10BeninBJ28
11Bosnia and HerzegovinaBA20
12BrazilBR29
13BulgariaBG22
14BurundiBI27
15Burkina FasoBF28
16CameroonCM27
17Cape VerdeCV25
18Central African RepublicCF27
19ChadTD27
20ComorosKM27
21CongoCG27
22Costa RicaCR22
23CroatiaHR21
24CyprusCY28
25Czech RepublicCZ24
26DenmarkDK18
27DjiboutiDJ27
28Dominican RepublicDO28
29EgyptEG29
30El SalvadorSV28
32Equatorial GuineaGQ27
33EstoniaEE20
34Falkland IslandsFK18
35Faroe IslandsFO18
36FinlandFI18
37FranceFR27
38GabonGA27
39GeorgiaGE22
40GermanyDE22
41GibraltarGI23
42GreeceGR27
43GreenlandGL18
44GuatemalaGT28
45Guinea-BissauGW25
46VaticanVA22
47HondurasHN28
48HungaryHU28
49IcelandIS26
50IranIR26
51IraqIQ23
52IrelandIE22
53IsraelIL23
54ItalyIT27
55Ivory CoastCI28
56JordanJO30
57KazakhstanKZ20
58KosovoXK20
59KuwaitKW30
60LatviaLV21
61LebanonLB28
62LibyaLY25
63LiechtensteinLI21
64LithuaniaLT20
65LuxembourgLU20
66MadagascarMG27
67MaliML28
68MaltaMT31
69MauritaniaMR27
70MauritiusMU30
71MoldovaMD24
72MonacoMC27
73MongoliaMN20
74MontenegroME22
75MoroccoMA28
76MozambiqueMZ25
77NetherlandsNL18
78NicaraguaNI28
79NigerNE28
80North MacedoniaMK19
81NorwayNO15
82PakistanPK24
83PalestinePS29
84PolandPL28
85PortugalPT25
86QatarQA29
87RomaniaRO24
88RussiaRU33
89Saint LuciaLC32
90San MarinoSM27
91Sao Tome and PrincipeST25
92Saudi ArabiaSA24
93SenegalSN28
94SerbiaRS22
95SeychellesSC31
96Slovak RepublicSK24
97SloveniaSI19
98SomaliaSO23
99SpainES24
100SudanSD18
101Sultanate of OmanOM23
102SwedenSE24
103SwitzerlandCH21
104Timor-LesteTL23
105TogoTG28
106TunisiaTN24
107TurkeyTR26
108UkraineUA29
109United Arab EmiratesAE23
110United KingdomGB22
111Virgin Islands, BritishVG24
112YemenYE30
</details>

Development

Install dependencies

npm i

Test

npm run test

Build

npx tsc

Changelog

v 1.2.2

v 1.2.1

v 1.2.0

v 1.1.9

v 1.1.8

v 1.1.7

v 1.1.6

v 1.1.5

Added additional pattern validation Added more tests to improve test coverage Added support for new countries: Algeria, Angola, Benin, Burkina Faso, Burundi, Cameroon, Cape Verde, Central African Republic, Chad, Comoros, Congo, Equatorial Guinea, Gabon, Guinea-Bissau, Honduras, Iran, Ivory Coast, Madagascar, Mali, Mongolia, Morocco, Mozambique, Nicaragua, Niger, Russia, Senegal, Togo

v 1.1.4

Avoid Angular warnings for old CommonJS module usage (see https://angular.io/guide/build#configuring-commonjs-dependencies)

Replaced mocha and chai with JEST for tests

v 1.1.3

Added support for new countries: Vatican, Libya, Sao Tome and Principe, Sudan Updated length for LC Saint Lucia from 30 to 32

Added Tests Added Mocha and Chai for testing

v 1.1.2

Updated length for CR to 22 - @freddy36

Read more

IBAN Examples