Skip to content

Instantly share code, notes, and snippets.

@nalcorso
Last active May 18, 2016 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nalcorso/b627c7ee15a2f5982b2d8b8ca6792a02 to your computer and use it in GitHub Desktop.
Save nalcorso/b627c7ee15a2f5982b2d8b8ca6792a02 to your computer and use it in GitHub Desktop.
Check whether a given string is a valid ABN (Australian Business Number), written in Typescript.
// The following formula can be used to verify the ABN.
// To verify an ABN:
// 1. Subtract 1 from the first (left) digit to give a new eleven digit number
// 2. Multiply each of the digits in this new number by its weighting factor
// 3. Sum the resulting 11 products
// 4. Divide the total by 89, noting the remainder
// 5. If the remainder is zero the number is valid
isValidABN(value: string) :boolean {
var weights : number[] = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
value.replace(" ", "");
// check length is 11 digits
if (value.length != 11) return false;
// Multiply each digit by its weighting factor
var sum :number = 0;
for (var i = 0; i < weights.length; i++) {
// The first digit is reduced by 1
var digit :number = parseInt(value[i]) - (i==0?1:0);
sum += digit * weights[i];
}
// If the the sum is divisible by 89 the ABN is valid
return (sum % 89) == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment