Last active
February 29, 2024 03:30
-
-
Save airtonix/7129b0ed825a929bac8120efa6e9466f to your computer and use it in GitHub Desktop.
Validate Australian Business Number
This file contains hidden or 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
| /** | |
| * 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; | |
| } |
Author
If your ABN is anything spaced like
51 824 753 556format, the replacer function needs a 'g' flag, or use the new replaceAll.
abn = value.replace(/[^\d]/g, '');
👍
I would also use proper declarations, let and the digit variable is missing declaration.
function validateAustralianABN(value) {
const weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
let abn = value?.toString().replace(/[^\d]/g, '');
let result = false;
// check length is 11 digits
if (abn?.length === 11) {
// apply ato check method
let sum = 0,
weight,
digit;
for (let 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
If your ABN is anything spaced like
51 824 753 556format, the replacer function needs a 'g' flag, or use the new replaceAll.abn = value.replace(/[^\d]/g, '');sample ABNs