Skip to content

Commit ef06d1b

Browse files
committed
Merge branch 'main' of https://github.com/dom-baur/javascript-utils into feature/formatSwissIbanNumber
2 parents 3a016ae + bc3dd52 commit ef06d1b

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

CHANGELOG.md

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

2222
- `isValidSwissSocialInsuranceNumber` is now named properly
23+
- `isValidSwissIbanNumber` now also allows IBAN numbers with letters
2324

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

src/lib/swissStandards.spec.ts

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

1932
test.each([

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)