validation of a BSB (without going to live feed of bank data)

// check for a valid BSB
// pass field_name object, ln=7 for nnn nnn, 6=nnnnn, 0 = either

function check_bsb(field_name, ln) {

var valid = true, cnm = 0, csp=0, checkOK = ‘0123456789’;

var checkStr = field_name.value;

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

ch = checkStr.charAt(i);

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

cnm++;

else if ((ch == ‘ ‘ || ch == ‘-‘) && i == 3)

csp++;

else

valid = false;

};

var pstr = ‘Please form a valid BSB from 6 digits’;

if (ln == 7) {

valid = (valid && cnm == 6 && csp == 1) ? true : false;

pstr += ‘ separated with a spaceor dash \”-\” as a separator, ie., \”nnn nnn\” or \”nnn-nnn\”.’;

} else if (ln == 6) {

valid = (valid && cnm == 6 && csp == 0) ? true : false;

pstr += ‘ only, no spaces ie., \”nnnnnn\”.’;

} else if (ln == 0) {

valid = (valid && cnm == 6 && (csp == 0 || csp == 1)) ? true : false;

pstr += ‘,\nwith or without a space or dash \”-\” as\na separator, ie., \”nnn nnn\”, \”nnn-nnn\” or \”nnnnnn\”.’;

} else // error in calling parameters

valid = false;

if (!valid) {

field_name.focus();

alert(pstr);

};

return (valid);

}

Advertisements