Skip to content

Instantly share code, notes, and snippets.

@brunobuddy
Created June 8, 2018 09:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunobuddy/35d41b4cc6faf8ec636255ae3664b7dc to your computer and use it in GitHub Desktop.
Save brunobuddy/35d41b4cc6faf8ec636255ae3664b7dc to your computer and use it in GitHub Desktop.
TypeScript SIRET Validation Script (French company registration number)
// 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
}
@brunobuddy
Copy link
Author

brunobuddy commented Jun 8, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment