İçeriğe geç
IBAN Aracı

JavaScript IBAN Validator

Form doğrulaması için hazır, bağımlılıksız kod örnekleri. Kopyalayıp doğrudan projenizde kullanabilirsiniz. Tüm örnekler ISO 7064 MOD 97 mantığını kullanır.

Vanilla JavaScript

javascript
function normalizeIban(input) {
  return input.replace(/\biban\b/gi, "").replace(/[^a-z0-9]/gi, "").toUpperCase();
}

function mod97(iban) {
  const s = iban.slice(4) + iban.slice(0, 4);
  let r = 0;
  for (const c of s) {
    const v = c >= "A" && c <= "Z" ? String(c.charCodeAt(0) - 55) : c;
    for (const d of v) r = (r * 10 + Number(d)) % 97;
  }
  return r;
}

function isValidIban(input) {
  const iban = normalizeIban(input);
  if (!/^[A-Z]{2}\d{2}[A-Z0-9]+$/.test(iban)) return false;
  return mod97(iban) === 1;
}

console.log(isValidIban("TR33 0006 1005 1978 6457 8413 26")); // true

TypeScript

typescript
export function normalizeIban(input: string): string {
  return input.replace(/\biban\b/gi, "").replace(/[^a-z0-9]/gi, "").toUpperCase();
}

export function mod97(iban: string): number {
  const s = iban.slice(4) + iban.slice(0, 4);
  let r = 0;
  for (const c of s) {
    const v = c >= "A" && c <= "Z" ? String(c.charCodeAt(0) - 55) : c;
    for (const d of v) r = (r * 10 + Number(d)) % 97;
  }
  return r;
}

export function isValidIban(input: string): boolean {
  const iban = normalizeIban(input);
  return /^[A-Z]{2}\d{2}[A-Z0-9]+$/.test(iban) && mod97(iban) === 1;
}

React hook

tsx
import { useMemo, useState } from "react";
import { isValidIban, normalizeIban } from "./iban";

export function useIban(initial = "") {
  const [value, setValue] = useState(initial);
  const valid = useMemo(() => isValidIban(value), [value]);
  return { value, setValue, valid, normalized: normalizeIban(value) };
}

Zod şeması

typescript
import { z } from "zod";
import { isValidIban } from "./iban";

export const ibanSchema = z
  .string()
  .transform((v) => v.trim())
  .refine(isValidIban, { message: "Geçerli bir IBAN girin." });

HTML input pattern

html
<input
  type="text"
  inputmode="text"
  autocomplete="off"
  spellcheck="false"
  pattern="[A-Za-z]{2}[0-9]{2}[A-Za-z0-9 ]{11,34}"
  placeholder="TR33 0006 1005 1978 6457 8413 26"
/>

Test örnekleri (Vitest)

typescript
import { describe, it, expect } from "vitest";
import { isValidIban } from "./iban";

describe("isValidIban", () => {
  it("geçerli TR IBAN", () => {
    expect(isValidIban("TR33 0006 1005 1978 6457 8413 26")).toBe(true);
  });
  it("hatalı kontrol hanesi", () => {
    expect(isValidIban("TR00 0006 1005 1978 6457 8413 26")).toBe(false);
  });
  it("geçerli DE IBAN", () => {
    expect(isValidIban("DE89 3704 0044 0532 0130 00")).toBe(true);
  });
});

Not: pattern yalnızca kaba bir ön kontroldür. Gerçek doğrulama için MOD 97 fonksiyonunu kullanın. Sunucu tarafı için IBAN API uç noktalarını tercih edebilirsiniz.

İlgili araçlar