Created
October 5, 2010 02:57
-
-
Save adampetrovic/610895 to your computer and use it in GitHub Desktop.
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 | |
function validate_abn($abn){ | |
$abn = str_replace(" ", "", $abn); | |
$abn_char = preg_split('//', $abn, -1, PREG_SPLIT_NO_EMPTY); | |
$weight_factors = array(10,1,3,5,7,9,11,13,15,17,19); | |
foreach($abn_char as $digit){ | |
if(!is_numeric($digit)){ | |
$this->form_validation->set_message('validate_abn', 'The ABN you have entered contains non-numeric characters.'); | |
return FALSE; | |
} | |
} | |
if(sizeof($abn_char) != 11){ | |
$this->form_validation->set_message('validate_abn', 'The ABN you have entered is not the correct length.'); | |
return FALSE; | |
} | |
// Subtract 1 from first digit | |
$abn_char[0] = (int) $abn_char[0] - 1; | |
// Multiply each digit by its corresponding weighting factor | |
for($x = 0; $x < sizeof($abn_char); $x++){ | |
$abn_char[$x] = (int) $abn_char[$x] * $weight_factors[$x]; | |
} | |
// Sum Together Each Element in the ABN After Weight Factor Applied | |
$abn_sum = 0; | |
foreach($abn_char as $newdigit){ | |
$abn_sum += (int) $newdigit; | |
} | |
$this->form_validation->set_message('validate_abn', 'The ABN you have entered is not valid.'); | |
// Lastly, the sum of all weighted digits divded by 89 should have a remainder of 0 | |
return ($abn_sum % 89 == 0) ? TRUE : FALSE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment