Skip to content

Instantly share code, notes, and snippets.

@Hillsie
Created February 6, 2023 02:31
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 Hillsie/0aaa529e005693c69948f42867fe487a to your computer and use it in GitHub Desktop.
Save Hillsie/0aaa529e005693c69948f42867fe487a to your computer and use it in GitHub Desktop.
Validate ABN - (Australian Business Number)
function validateABN(abn: string): boolean {
/*
@name: validateABN
@param {string} abn
@returns {boolean}
@link https://abr.business.gov.au/Help/AbnFormat
*/
const weightingFactors = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
const cleanedABN = abn.replace(/\s/g, '')
if (cleanedABN.length !== 11) {
return false
}
let sum = 0
for (let i = 0; i < 11; i++) {
sum += (parseInt(cleanedABN[i]) - (i === 0 ? 1 : 0)) * weightingFactors[i]
}
return sum % 89 === 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment