-
Notifications
You must be signed in to change notification settings - Fork 7
Closed
Description
names are suggestions
isValidSwissSocialSecurityNumber(input: string)andformatSwissSocialSecurityNumber(input: string)isValidIbanNumber(input: string)andformatIBanNumber(input: string)
AHV
The formatting method should just check if it is 13 numbers and valid, if yes, return input 7561234567891 as 756.1234.5678.91
IBAN
The formatting method should be input: CH3900700115651849173 output CH39 0070 0115 6518 4917 3
We have this code in various projects for the AHV validation:
export const isAHVNumberValid = (socialInsuranceNumber: string): boolean => {
// Validation of Social insurance number with regex and checking of checksum
// Validation according to https://www.sozialversicherungsnummer.ch/aufbau-neu.htm
const regex = new RegExp(/[7][5][6][.][\d]{4}[.][\d]{4}[.][\d]{2}$/);
if (regex.test(socialInsuranceNumber)) {
//todo check checksum
const number = socialInsuranceNumber.substring(0, socialInsuranceNumber.length - 1);
const reversedNumber = number.split(".").join("").split("").reverse().join("");
let sum = 0;
for (let i = 0; i < reversedNumber.length; i++) {
sum += i % 2 == 0 ? Number(reversedNumber[i]) * 3 : Number(reversedNumber[i]) * 1;
}
const checksum = Math.ceil(sum / 10) * 10 - sum;
const checknumber = parseInt(socialInsuranceNumber[socialInsuranceNumber.length - 1]);
if (checksum !== checknumber) {
return false;
} else {
return true;
}
} else {
return false;
}
};
Metadata
Metadata
Assignees
Labels
No labels