From 2e8fe83606eb2f548b52a835859a65159db3dc51 Mon Sep 17 00:00:00 2001 From: Dominic Baur Date: Thu, 11 Sep 2025 10:49:09 +0200 Subject: [PATCH 1/4] Change regex schema for isValidSwissIbanNumber --- src/lib/swissStandards.spec.ts | 16 ++++++++++++---- src/lib/swissStandards.ts | 4 ++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/lib/swissStandards.spec.ts b/src/lib/swissStandards.spec.ts index 30f3df6..21b30d2 100644 --- a/src/lib/swissStandards.spec.ts +++ b/src/lib/swissStandards.spec.ts @@ -11,8 +11,16 @@ describe("Swiss standards test", () => { ["CH93 0000 0000 0000 0000 1", false], ["ch93 0076 2011 6238 5295 7", false], ["DE93 0076 2011 6238 5295 7", false], - ])("check if this swiss IBAN is valid or not", (unformattedIbanNumber, expected) => { - expect(isValidSwissIbanNumber(unformattedIbanNumber)).toBe(expected); + ])("check if this swiss IBAN is valid or not", (iBanNumberToCheck, expected) => { + expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected); + }); + + test.each([ + [null as unknown as string, false], + [undefined as unknown as string, false], + ["CH35 0023 0230 1234 5601 X", true], + ])("check if this siwss IBAN with letters is valid or not", (iBanNumberToCheck, expected) => { + expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected); }); test.each([ @@ -26,7 +34,7 @@ describe("Swiss standards test", () => { ["756.1234.5678.91", false], ["test756.9217.0769.85", false], ["7.56..9217...0769.85", false], - ])("check if the social insurance number is valid or not", (ahvNumber, expected) => { - expect(isValidSwissSocialInsuranceNumber(ahvNumber)).toBe(expected); + ])("check if the social insurance number is valid or not", (socialInsuranceNumberToCheck, expected) => { + expect(isValidSwissSocialInsuranceNumber(socialInsuranceNumberToCheck)).toBe(expected); }); }); diff --git a/src/lib/swissStandards.ts b/src/lib/swissStandards.ts index cfcaa68..cd1b9c2 100644 --- a/src/lib/swissStandards.ts +++ b/src/lib/swissStandards.ts @@ -16,9 +16,9 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean { // 2. Define allowed strict formats // - with spaces: "CHXX XXXX XXXX XXXX XXXX X" - const compactIbanNumberWithWhiteSpaces = new RegExp(/^CH\d{2}(?: \d{4}){4} \d{1}$/); + const compactIbanNumberWithWhiteSpaces = new RegExp(/^CH\d{2}(?:\s[A-Z0-9]{4}){4}\s[A-Z0-9]{1}$/); // - without spaces: "CHXXXXXXXXXXXXXXXXXXX" - const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH\d{19}$/); + const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH\d{2}[A-Z0-9]{17}$/); // 3. Check if input matches one of the allowed formats if (!compactIbanNumberWithWhiteSpaces.test(ibanNumber) && !compactIbanNumberWithoutWhiteSpaces.test(ibanNumber)) { From b03a93a7c4068923962961158f8e0b36bd2bd78b Mon Sep 17 00:00:00 2001 From: Dominic Baur Date: Wed, 1 Oct 2025 08:48:31 +0200 Subject: [PATCH 2/4] Fixed the regex test check --- src/lib/swissStandards.spec.ts | 11 ++++++++--- src/lib/swissStandards.ts | 10 +++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/lib/swissStandards.spec.ts b/src/lib/swissStandards.spec.ts index 21b30d2..ec29423 100644 --- a/src/lib/swissStandards.spec.ts +++ b/src/lib/swissStandards.spec.ts @@ -11,15 +11,20 @@ describe("Swiss standards test", () => { ["CH93 0000 0000 0000 0000 1", false], ["ch93 0076 2011 6238 5295 7", false], ["DE93 0076 2011 6238 5295 7", false], - ])("check if this swiss IBAN is valid or not", (iBanNumberToCheck, expected) => { + ])("check if the swiss IBAN number is valid or not", (iBanNumberToCheck, expected) => { expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected); }); test.each([ [null as unknown as string, false], [undefined as unknown as string, false], - ["CH35 0023 0230 1234 5601 X", true], - ])("check if this siwss IBAN with letters is valid or not", (iBanNumberToCheck, expected) => { + ["CH3400762ABC123DEF456", true], + ["CH34 0076 2ABC 123D EF45 6", true], + ["Some random string", false], + ["DE34 0076 2ABC 123D EF45 3", false], + ["CH34 0076 2ABC 123D EF45 \n6", false], + ["CH34 0076 2ABC 123D EF45 !", false], + ])("check if the siwss IBAN number with letters is valid or not", (iBanNumberToCheck, expected) => { expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected); }); diff --git a/src/lib/swissStandards.ts b/src/lib/swissStandards.ts index cd1b9c2..a9a8e36 100644 --- a/src/lib/swissStandards.ts +++ b/src/lib/swissStandards.ts @@ -20,8 +20,8 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean { // - without spaces: "CHXXXXXXXXXXXXXXXXXXX" const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH\d{2}[A-Z0-9]{17}$/); - // 3. Check if input matches one of the allowed formats - if (!compactIbanNumberWithWhiteSpaces.test(ibanNumber) && !compactIbanNumberWithoutWhiteSpaces.test(ibanNumber)) { + // 3. Check if the input matches one of the allowed formats + if (!(compactIbanNumberWithWhiteSpaces.test(ibanNumber) || compactIbanNumberWithoutWhiteSpaces.test(ibanNumber))) { return false; } @@ -46,7 +46,7 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean { } /** - * Validation of social insurance number with checking the checksum + * Validation of a social insurance number with checking the checksum * Validation according to https://www.sozialversicherungsnummer.ch/aufbau-neu.htm * @param socialInsuranceNumber The social insurance number to check * Must be in one of the following formats: @@ -55,13 +55,13 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean { * @returns The result if the social insurance number is valid or not */ export function isValidSwissSocialInsuranceNumber(socialInsuranceNumber: string): boolean { - // 1. Check if input is empty or only whitespace + // 1. Check if the input is empty or only a whitespace if (isNullOrWhitespace(socialInsuranceNumber)) { return false; } /** - * 2. Check if input matches accepted formats: + * 2. Check if the input matches one of the accepted formats: * - With dots: 756.XXXX.XXXX.XX * - Without dots: 756XXXXXXXXXX */ From 7e8c67f7e59a0145b2c7818c55f9e5f50525b975 Mon Sep 17 00:00:00 2001 From: Dominic Baur Date: Wed, 1 Oct 2025 08:53:20 +0200 Subject: [PATCH 3/4] Updated the changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ec93da..3dfec4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - `isValidSwissSocialInsuranceNumber` is now named properly +- `isValidSwissIbanNumber` now also allows IBAN numbers with letters ## [2.1.0] - 2025-09-03 From d4a9db6ac15790051943c6cf1237647fe9662ce8 Mon Sep 17 00:00:00 2001 From: Dominic Baur Date: Wed, 1 Oct 2025 14:42:32 +0200 Subject: [PATCH 4/4] Improved regex --- src/lib/swissStandards.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/swissStandards.ts b/src/lib/swissStandards.ts index a9a8e36..3fd7d13 100644 --- a/src/lib/swissStandards.ts +++ b/src/lib/swissStandards.ts @@ -16,9 +16,9 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean { // 2. Define allowed strict formats // - with spaces: "CHXX XXXX XXXX XXXX XXXX X" - const compactIbanNumberWithWhiteSpaces = new RegExp(/^CH\d{2}(?:\s[A-Z0-9]{4}){4}\s[A-Z0-9]{1}$/); + const compactIbanNumberWithWhiteSpaces = new RegExp(/^CH[0-9]{2} [0-9]{4} [0-9][A-Z0-9]{3} [A-Z0-9]{4} [A-Z0-9]{4} [A-Z0-9]$/); // - without spaces: "CHXXXXXXXXXXXXXXXXXXX" - const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH\d{2}[A-Z0-9]{17}$/); + const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH[0-9]{7}[A-Z0-9]{12}$/); // 3. Check if the input matches one of the allowed formats if (!(compactIbanNumberWithWhiteSpaces.test(ibanNumber) || compactIbanNumberWithoutWhiteSpaces.test(ibanNumber))) {