Skip to content

Commit 2e8fe83

Browse files
committed
Change regex schema for isValidSwissIbanNumber
1 parent 256fda4 commit 2e8fe83

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

src/lib/swissStandards.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ 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 this swiss IBAN 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+
["CH35 0023 0230 1234 5601 X", true],
22+
])("check if this siwss IBAN with letters is valid or not", (iBanNumberToCheck, expected) => {
23+
expect(isValidSwissIbanNumber(iBanNumberToCheck)).toBe(expected);
1624
});
1725

1826
test.each([
@@ -26,7 +34,7 @@ describe("Swiss standards test", () => {
2634
["756.1234.5678.91", false],
2735
["test756.9217.0769.85", false],
2836
["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);
37+
])("check if the social insurance number is valid or not", (socialInsuranceNumberToCheck, expected) => {
38+
expect(isValidSwissSocialInsuranceNumber(socialInsuranceNumberToCheck)).toBe(expected);
3139
});
3240
});

src/lib/swissStandards.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ 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\d{2}(?:\s[A-Z0-9]{4}){4}\s[A-Z0-9]{1}$/);
2020
// - without spaces: "CHXXXXXXXXXXXXXXXXXXX"
21-
const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH\d{19}$/);
21+
const compactIbanNumberWithoutWhiteSpaces = new RegExp(/^CH\d{2}[A-Z0-9]{17}$/);
2222

2323
// 3. Check if input matches one of the allowed formats
2424
if (!compactIbanNumberWithWhiteSpaces.test(ibanNumber) && !compactIbanNumberWithoutWhiteSpaces.test(ibanNumber)) {

0 commit comments

Comments
 (0)