jcadima
6/22/2015 - 1:32 PM

Validate SSN

Validate SSN

// THE HTML :
<form id="onecardform" name="frm" action="process.php" method="post" onsubmit="return validateForm2()">
    // form elements go here

</form>



// JS:
function validateForm2() {
    var mobile = document.forms["onecardform"]["mobile"].value;
	var ssn    = document.forms["onecardform"]["social"].value;
	var MM     = document.forms["onecardform"]["MM"].value;
	var DD     = document.forms["onecardform"]["DD"].value;    
    var YYYY   = document.forms["onecardform"]["YYYY"].value;
    
    if (mobile == null || mobile == "" || validareacode2() == false ) {
        sweetAlert("Phone Area code error", "Please enter a valid area code", "error");
        return false;
    }

    if (ssn == null || ssn == ""  ||  validateSSN(ssn) == false ) {
        sweetAlert("SSN error",  "We need a valid social security number to verify your information", "error");
        return false;
    }	
    
    if ( MM == null || MM == "" || MM > 12 ) {
        sweetAlert("Date of birth Month error",  "Please enter a valid Month digit [1 - 12]", "error");
        return false;        
    }

    if ( DD == null || DD == "" || DD > 31 ) {
        sweetAlert("Date of birth error",  "Please enter a valid Day digit [1 - 31]", "error");
        return false;        
    }

    if (YYYY == null || YYYY == "" || validateDOB(MM, DD, YYYY) == false ) {
       sweetAlert("Date of Birth error" ,"Must be 18 years old to apply", "error");
        return false;
    }    
    
    
}




// validate social security number with ssn parameter as string
function validateSSN(ssn) {
    // SSN without dashes:
    var ssn_string = ssn.replace(/-/g, ""); // 123456789
    var result     = false;
    // find area number (1st 3 digits)
    var area1 = parseInt(ssn_string.substring(0, 3));  // 123
    console.log("AREA1 : " + area1);
    var area2 = parseInt(ssn_string.substring(3, 5));  // 45
    console.log("AREA2 : " + area2);
    var area3 = parseInt(ssn_string.substring(5, 9));  // 6789
    console.log("AREA3 : " + area3);
    var area_range = area1.between(900,999); // area range can't be a number between 900 - 999
    // no set can start with zero AND disallow Satan's minions from becoming residents of the US AND it's over 900
    if ( ssn_string.match(/^[1-9][0-9]{2}[1-9][0-9]{1}[1-9][0-9]{3}/) && area1 !== 666 && area1 !== 000 && area_range == false && area2 !== 00 && area3 !== 0000 ) {
        result = true;
    }
    return result;
}


// prototype function to be used to get specific range of number areas
Number.prototype.between = function (min, max) {
    return this > min && this < max;
};