Skip to content

Instantly share code, notes, and snippets.

@teebu
Forked from airtonix/validateABN.js
Created March 30, 2023 15:47
Show Gist options
  • Save teebu/379573309ad39e0699dd09ccf9585906 to your computer and use it in GitHub Desktop.
Save teebu/379573309ad39e0699dd09ccf9585906 to your computer and use it in GitHub Desktop.
Validate Australian Business Number
/**
* Checks ABN for validity using the published ABN checksum algorithm.
* @author Guy Carpenter
* @license http://www.clearwater.com.au/code None
* @param {String|Number} value abn to validate
* @return {Boolean} Is ABN Valid
*/
function validateABN (value) {
var weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19],
abn = value.replace(/[^\d]/g, ''),
result = false;
// check length is 11 digits
if (abn.length === 11) {
// apply ato check method
var sum = 0,
weight;
for (var index = 0; index <= weights.length - 1; index++) {
weight = weights[index];
digit = abn[index] - (index ? 0 : 1);
sum += weight * digit;
}
result = sum % 89 === 0;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment