Created
December 7, 2016 08:36
-
-
Save nahanil/a558ac0070dc46ca132f94dc308cbbe6 to your computer and use it in GitHub Desktop.
Validate an ABN Australian Business Number
This file contains 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
/** | |
* https://abr.business.gov.au/HelpAbnFormat.aspx | |
*/ | |
function validateAbn(abn) { | |
// The Australian Business Number (ABN) is a unique 11 digit identifier issued to all entities registered in the Australian Business Register (ABR). | |
var digits = abn.toString().split(''); | |
if (digits.length != 11) { | |
return false; | |
} | |
// To verify an ABN: | |
// 1 - Subtract 1 from the first (left-most) digit of the ABN to give a new 11 digit number | |
digits[0] -= 1; | |
// 2 - Multiply each of the digits in this new number by a "weighting factor" based on its position as shown in the table below | |
var weight = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]; | |
for (i=0; i < 11; i++) { | |
digits[i] = digits[i] * weight[i]; | |
} | |
// 3 - Sum the resulting 11 products | |
// 4 - Divide the sum total by 89, noting the remainder | |
// 5 - If the remainder is zero the number is a valid ABN | |
return digits.reduce(function(a, b){ return a + b; },0) % 89 == 0; | |
} |
This file contains 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
<?php | |
/** | |
* https://abr.business.gov.au/HelpAbnFormat.aspx | |
*/ | |
function validateAbn($abn) { | |
// The Australian Business Number (ABN) is a unique 11 digit identifier issued to all entities registered in the Australian Business Register (ABR). | |
$digits = str_split($abn); | |
if (count($digits) != 11) { | |
return false; | |
} | |
// To verify an ABN: | |
// 1 - Subtract 1 from the first (left-most) digit of the ABN to give a new 11 digit number | |
$digits[0] -= 1; | |
// 2 - Multiply each of the digits in this new number by a "weighting factor" based on its position as shown in the table below | |
$weight = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19); | |
for ($i=0; $i < 11; $i++) { | |
$digits[$i] = $digits[$i] * $weight[$i]; | |
} | |
// 3 - Sum the resulting 11 products | |
// 4 - Divide the sum total by 89, noting the remainder | |
// 5 - If the remainder is zero the number is a valid ABN | |
return array_sum($digits) % 89 == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment