Skip to content

Commit bc3dd52

Browse files
authored
Fix the isValidSwissIbanNumber function also allows IBAN numbers with letters (#83)
Fix for the swiss standards function isValidSwissIbanNumber Issue #77
1 parent 256fda4 commit bc3dd52

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### Fixed
2020

2121
- `isValidSwissSocialInsuranceNumber` is now named properly
22+
- `isValidSwissIbanNumber` now also allows IBAN numbers with letters
2223

2324
## [2.1.0] - 2025-09-03
2425

src/lib/swissStandards.spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ describe("Swiss standards test", () => {
1111
["CH93 0000 0000 0000 0000 1", false],
1212
["ch93 0076 2011 6238 5295 7", false],
1313
["DE93 0076 2011 6238 5295 7", false],
14-
])("check if this swiss IBAN is valid or not", (unformattedIbanNumber, expected) => {
15-
expect(isValidSwissIbanNumber(unformattedIbanNumber)).toBe(expected);
14+
])("check if the swiss IBAN number is valid or not", (iBanNumberToCheck, expected) => {
15+
expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected);
16+
});
17+
18+
test.each([
19+
[null as unknown as string, false],
20+
[undefined as unknown as string, false],
21+
["CH3400762ABC123DEF456", true],
22+
["CH34 0076 2ABC 123D EF45 6", true],
23+
["Some random string", false],
24+
["DE34 0076 2ABC 123D EF45 3", false],
25+
["CH34 0076 2ABC 123D EF45 \n6", false],
26+
["CH34 0076 2ABC 123D EF45 !", false],
27+
])("check if the siwss IBAN number with letters is valid or not", (iBanNumberToCheck, expected) => {
28+
expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected);
1629
});
1730

1831
test.each([
@@ -26,7 +39,7 @@ describe("Swiss standards test", () => {
2639
["756.1234.5678.91", false],
2740
["test756.9217.0769.85", false],
2841
["7.56..9217...0769.85", false],
29-
])("check if the social insurance number is valid or not", (ahvNumber, expected) => {
30-
expect(isValidSwissSocialInsuranceNumber(ahvNumber)).toBe(expected);
42+
])("check if the social insurance number is valid or not", (socialInsuranceNumberToCheck, expected) => {
43+
expect(isValidSwissSocialInsuranceNumber(socialInsuranceNumberToCheck)).toBe(expected);
3144
});
3245
});

src/lib/swissStandards.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean {
1616

1717
// 2. Define allowed strict formats
1818
// - with spaces: "CHXX XXXX XXXX XXXX XXXX X"
19-
const compactIbanNumberWithWhiteSpaces = new RegExp(/^CH\d{2}(?: \d{4}){4} \d{1}$/);
19+
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]$/);
2020
// - without spaces: "CHXXXXXXXXXXXXXXXXXXX"
21-
const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH\d{19}$/);
21+
const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH[0-9]{7}[A-Z0-9]{12}$/);
2222

23-
// 3. Check if input matches one of the allowed formats
24-
if (!compactIbanNumberWithWhiteSpaces.test(ibanNumber) && !compactIbanNumberWithoutWhiteSpaces.test(ibanNumber)) {
23+
// 3. Check if the input matches one of the allowed formats
24+
if (!(compactIbanNumberWithWhiteSpaces.test(ibanNumber) || compactIbanNumberWithoutWhiteSpaces.test(ibanNumber))) {
2525
return false;
2626
}
2727

@@ -46,7 +46,7 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean {
4646
}
4747

4848
/**
49-
* Validation of social insurance number with checking the checksum
49+
* Validation of a social insurance number with checking the checksum
5050
* Validation according to https://www.sozialversicherungsnummer.ch/aufbau-neu.htm
5151
* @param socialInsuranceNumber The social insurance number to check
5252
* Must be in one of the following formats:
@@ -55,13 +55,13 @@ export function isValidSwissIbanNumber(ibanNumber: string): boolean {
5555
* @returns The result if the social insurance number is valid or not
5656
*/
5757
export function isValidSwissSocialInsuranceNumber(socialInsuranceNumber: string): boolean {
58-
// 1. Check if input is empty or only whitespace
58+
// 1. Check if the input is empty or only a whitespace
5959
if (isNullOrWhitespace(socialInsuranceNumber)) {
6060
return false;
6161
}
6262

6363
/**
64-
* 2. Check if input matches accepted formats:
64+
* 2. Check if the input matches one of the accepted formats:
6565
* - With dots: 756.XXXX.XXXX.XX
6666
* - Without dots: 756XXXXXXXXXX
6767
*/

0 commit comments

Comments
 (0)