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 diff --git a/src/lib/swissStandards.spec.ts b/src/lib/swissStandards.spec.ts index 30f3df6..ec29423 100644 --- a/src/lib/swissStandards.spec.ts +++ b/src/lib/swissStandards.spec.ts @@ -11,8 +11,21 @@ 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 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], + ["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); }); test.each([ @@ -26,7 +39,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..3fd7d13 100644 --- a/src/lib/swissStandards.ts +++ b/src/lib/swissStandards.ts @@ -16,12 +16,12 @@ 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[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{19}$/); + const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH[0-9]{7}[A-Z0-9]{12}$/); - // 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 */