Created
June 8, 2018 09:50
-
-
Save brunobuddy/35d41b4cc6faf8ec636255ae3664b7dc to your computer and use it in GitHub Desktop.
TypeScript SIRET Validation Script (French company registration number)
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
// Returns true if valid, false if not. Note that registrationNumber param is a string (usually provided by HTML imput) | |
validateRegistrationNumber(registrationNumber: string): boolean { | |
if (registrationNumber.length !== 14) { | |
return false | |
} | |
let sum = 0 | |
let digit: number | |
for (let i = 0; i < registrationNumber.length; i++) { | |
if (i % 2 === 0) { | |
digit = parseInt(registrationNumber.charAt(i), 10) * 2 | |
digit = digit > 9 ? (digit -= 9) : digit | |
} else { | |
digit = parseInt(registrationNumber.charAt(i), 10) | |
} | |
sum += digit | |
} | |
return sum % 10 === 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A small TypeScript snippet to validate french company registration numbers, also called SIRET.
We are using Luhn Algorithm to validate the number. As the last SIRET Validation script I found was written in 2003 with the JavaScript of the time, I decided to refresh it a little.
Note that the function parameter is a string and not a number as it will probably come from an HTML input "text" (I found it more user-friendly than the input "number" for this kind of values). If you want to have an input "number", just replace
registrationNumber
by
registrationNumber.toString()
within the function.