validation of drivers license numbers

// This is some old (and ugly) code from long ago
// some of these validation functions are difficult to do as Regex, so over years used these over and over to do pre-validation on client, saving user’s round trips from mis-keyed numbers.
// though ugly, maybe it might help someone now – but advise you to clean it up / rewrite it first.

function validateDriverLic(field_name, field_description) {
    var i, ch, cnt_chr, cnt_num;
    var all_zero = true;
    var checkOK = “0123456789”;
    cnt_chr = 0; 
    cnt_num = 0;
    var D_Licence = field_name.value;
    if (D_Licence.length>9) { //the length should be 9
        // alert(“The maximum length of a Drivers Licence number is 9 characters”);
        alert(“Invalid ” + field_description);
        field_name.focus(); 
        return false; 
    }

    for (i = 0; i < D_Licence.length; i++) {
        ch = D_Licence.charAt(i);
        if (((i==2) || (i==3)) && (checkOK.indexOf(ch)==-1))
        {
             //must third and fourth char be numbers
            // alert(“3rd and 4th characters of Drivers Licence must be numeric.”);
            alert(“Invalid ” + field_description);
            field_name.focus(); 
            return false; 
        } 
        if (ch!=’0′) all_zero = false;
        if (checkOK.indexOf(ch)!=-1) //(ch.isnumeric)
            cnt_num++;
        else
            cnt_chr++;
        }
        if (cnt_num < 4)
        {
            //must have at least 4 number
            // alert(“Drivers Licence should include at least 4 Numeric characters”);
            alert(“Invalid ” + field_description);
            field_name.focus(); 
            return false; 
        } 
        if (cnt_chr > 2)
        {
            //must not have more that 2 char 
            // alert(“Drivers Licence should not include more than 2 alpha characters”);
            alert(“Invalid ” + field_description);
            field_name.focus(); 
            return false; 
        }
        if (cnt_chr == 9)
        {
            //must not have all char
            // alert(“Drivers Licence can not have all alpha characters”);
            alert(“Invalid ” + field_description);
            field_name.focus(); 
            return false; 
        }
        if (all_zero == true)
        {
            //must not have all zero
            // alert(“Drivers Licence number can not be all zero\’s”);
            alert(“Invalid ” + field_description);
            field_name.focus(); 
            return false; 
        }
}

Advertisements