Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 17 additions & 4 deletions src/lib/swissStandards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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);
});
});
14 changes: 7 additions & 7 deletions src/lib/swissStandards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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:
Expand All @@ -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
*/
Expand Down
Loading