Client side javascript validation for ABN and ACN

// check for valid ABN
// if fmt_str true, add ABR formatting info.

function check_ABN(field_name, field_description, fmt_str) {

var abn_fmt_str = (fmt_str) ? ‘\nThe ABN is a unique 11 digit number formed from a 9 digit unique identifier and two prefix check digits.\nThe Australian Business Register structure an ABN as XX XXX XXX XXX to assist readability.’ : ”;

var abn = ”, chk = 0, k=11, checkOK = “0123456789”, ch=”;

var weighting = new Array(19,17,15,13,11, 9, 7, 5, 3, 1,10)

var checkStr = field_name.value, allValid = true;

for (i = 0; i < checkStr.length; i++) {

ch = checkStr.charAt(i);

if (checkOK.indexOf(ch) != -1) {

if (k == 11) {

k–;

chk += weighting[k]*(parseInt(ch)-1);

}

else

if (k– > 0) chk += weighting[k]*parseInt(ch);

abn += ch;

}

}

chk = chk/89 – Math.floor(chk/89)

if (abn.length != 11 || k < 0 || chk != 0) {

field_name.focus();

alert(‘Please check field \”‘ + field_description + ‘\” is a valid ABN.\n’+abn_fmt_str);

return (false);

}

return (true);

}

// check for valid ACN
// if fmt_str true, add ASIC formatting info.

function check_ACN(field_name, field_description, fmt_str) {

var acn_fmt_str = (fmt_str) ? ‘\nASIC has adopted a convention of always printing and displaying the ACN in the format nnn nnn nnn ie three blocks of three characters separated by a blank. This is to assist readability and the inserted blanks do not form part of the ACN.’ : ”;

var acn = ”, chk = 0, k=9, checkOK = “0123456789”, ch=”;

var checkStr = field_name.value, allValid = true;

for (i = 0; i < checkStr.length; i++) {

ch = checkStr.charAt(i);

if (checkOK.indexOf(ch) != -1) {

if (k– > 0) chk += k*parseInt(ch);

acn += ch;

}

}

ch = chk.toString(10);

chk = 10-(parseInt(ch.charAt(ch.length-1)));

if (chk == 10) chk = 0;

if (acn.length != 9 || k < 0 || chk != acn.charAt(8)) {

field_name.focus();

alert(‘Please check field \”‘ + field_description + ‘\” is a valid ACN.\n’+acn_fmt_str);

return (false);

}

return (true);

}

Advertisements