Skip to content

Instantly share code, notes, and snippets.

@benjaminalldridge
Created January 5, 2024 02:02
Show Gist options
  • Select an option

  • Save benjaminalldridge/cade69cc81393a490e6564628a14f1e0 to your computer and use it in GitHub Desktop.

Select an option

Save benjaminalldridge/cade69cc81393a490e6564628a14f1e0 to your computer and use it in GitHub Desktop.
ABN Validator
/**
* Validate a provided ABN via 3 methods: numerically, via API, and (optionally) mathematically
*
* @param {string} abn The ABN to validate. White space is trimmed at runtime.
* @returns {bool}
*/
function validateAbn(abn) {
// ABR requires a guid to access the API
const guid = '[FILL IN HERE]';
// Trim out whitespace before we do anything else
let abnTrimmed = abn.replace(/\s/g,'');
// Check if the ABN format is correct: exactly 11 digits
const formatRegex = /^\d{11}$/;
if (!formatRegex.test(abnTrimmed)) {
// If the regex does not validate, do not proceed
console.warn('ABN Validator :: the ABN provided does not conform to the appropriate format.');
return false;
}
// If it's valid numerically, hit the API
$.get('https://abr.business.gov.au/json/AbnDetails.aspx?abn=' + abnTrimmed + '&guid=' + guid)
.done(function(response) {
// Break off the 'P' part of the JSONP packet
const jsonPart = response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1);
// Parse the JSON string to an object
try {
// Parse it
let jsonData = JSON.parse(jsonPart);
// Is there a message? If so, tell us
if(jsonData.Message != '') {
console.warn('ABN Validator :: ' + jsonData.Message);
}
// Is there an active status? If not, is no good
if(jsonData.AbnStatus !== 'Active') {
// Active is the only affirmative status, fail if not matched
console.warn('ABN Validator :: the ABN provided is not valid.')
return false;
}
else {
// This should be the only positive return from the API
console.info('ABN Validator :: the ABN provided is valid.')
return true;
}
} catch (error) {
// We have caught an error parsing the ABR JSONP, let us know
console.error('ABN Validator :: error parsing JSONP response.', error);
console.warn('ABN Validator :: reverting to checksum validation.');
// Try the old fashioned way!
const weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
// Apply ATO check method
let sum = 0,
weight,
digit;
for (let index = 0; index <= weights.length - 1; index++) {
weight = weights[index];
digit = abnTrimmed[index] - (index ? 0 : 1);
sum += weight * digit;
}
// All affirmative results are divisible by 89
if(sum % 89 === 0) {
// Inform the operator that the ABN is *mathematically* valid
alert('ABN Validator :: ABN is mathematically valid. Check with ABR if it is currently active.');
console.warn('ABN Validator :: checksum validation passed, but the provided ABN may not be active with ABR.');
return true;
}
else {
// If we are here, it's no good
console.warn('ABN Validator :: the provided ABN is not mathematically valid.');
return false;
}
}
})
.fail(function(data) { // There was a failure somewhere, die
console.warn('ABN Validator :: there was no sane pathway forward; aborting.')
return false;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment