Last active
October 11, 2021 15:44
-
-
Save jonaszuberbuehler/c6f37fc3eff44a3f69b8115e6593cf64 to your computer and use it in GitHub Desktop.
Swiss SV Number Validation / AHV Nummer Validierung (see https://www.bsv.admin.ch/bsv/de/home/sozialversicherungen/ahv/grundlagen-gesetze/ahv-nummer.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValidateSVNumber(toValidate) { | |
const number = toValidate.replaceAll('.', ''); | |
if (number.length < 13) { | |
return false; | |
} | |
const parts = number.split(''); | |
const checksum = parseInt(parts[parts.length - 1]); | |
const sum = parts.reverse().slice(1).reduce((sum, number, index) => { | |
if (index % 2 === 0) { | |
return sum + parseInt(number) * 3; | |
} | |
return sum + parseInt(number); | |
}, 0); | |
return (sum + checksum) % 10 === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment