Project Claire: JavaScript ~ External Validation File

From PikaDocs

Last modified: July 2, 2005

The following javascript is located in the validation.js file located in the /js subdirectory. The script includes a series of functions to validate the SSN (Social Security Number), DOB (Date of Birth) and Phone form fields located at various locations within the Pika CMS.

function validate() {
if (document.form1.last_name.value.length < 1 && document.form1.ssn.value.length < 1
&& document.form1.phone.value.length < 1) {
alert("Enter something I can search for!");
return false;
}
if (document.form1.ssn.value.length > 1)
return SSNValidation(form1.ssn.value);
else
return true;

}
function SSNValidation(ssn) {
var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
var numDashes = ssn.split('-').length - 1;
if (matchArr == null || numDashes == 1) {
alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
msg = "does not appear to be valid";
}
else 
if (parseInt(matchArr[1],10)==0) {
alert("Invalid SSN: SSN's can't start with 000.");
msg = "does not appear to be valid";
}
else {

   }

}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");

 }


function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var re = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    if (re.test(entry)) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1), 10);
        day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        var testDate = new Date(yr, mo-1, day);
        alert(testDate)
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() == yr) {
                    return true;
                } else {
                    alert("There is a problem with the year entry.");
                }
            } else {
                alert("There is a problem with the month entry.");
            }
        } else {
            alert("There is a problem with the date entry.");
        }
    } else {
        alert("Incorrect date format. Enter as mm/dd/yyyy.");
    }

    return false;
}

function validateDate(fld) {
    if (!checkDate(fld)) {
        // focus if validation fails
        fld.focus();
        fld.select();
    }
}



function checkDate(fld) {
    var mo, day, yr;
    var now = new Date();
    var entry = fld.value;
    var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
    var valid = (reLong.test(entry)) || (reShort.test(entry));
    if (valid) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1), 10);
        day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        // handle two-digit year
        if (yr < 100) {
            var today = new Date();
            // get current century floor (e.g., 2000)
            var currCent = parseInt(today.getFullYear() / 100) * 100;
            // two digits up to this year + 15 expands to current century
            var threshold = (today.getFullYear() + 15) - currCent;
            if (yr > threshold) {
                yr += currCent - 100;
            } else {
                yr += currCent;
            }
        }
        var testDate = new Date(yr, mo-1, day);
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() >= 1885 && testDate.getFullYear()  <= now.getFullYear()) {
                    // fill field with database-friendly format
                    fld.value = mo + "/" + day + "/" + yr;
                    return true;
                } else {
                    alert("There is a problem with the year entry. This person is too old to be alive or too young to be a client. Either counsel the applicant to consider moving to Florida where such things are acceptable practice, or correct your date entry. It's your call.");
                }
            } else {
                alert("There is a problem with the month entry.");
            }
        } else {
            alert("There is a problem with the date entry.");
        }
    } else {
        alert("Incorrect date format. Enter as mm/dd/yyyy.");
    }
    return false;
}


Return to Table of Contents

Related articles and files: