jcadima
6/14/2015 - 9:35 PM

Form Validation, DOB, phone, SSN, area code

Form Validation, DOB, phone, SSN, area code

// this is for hardcore phone validation including international numbers
// ref:  http://www.smartwebby.com/DHTML/us_phone_no_validation.asp


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 == "" || validareacode() == 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;
    }    
    
    
}

function validareacode(mobile) { 
//US Invalid Area Codes
var result = true;
var AreaCode=new Array(204,211,226,236,242,246,249,250,264,268,284,306,340,343,345,365,387,403,411,416,418,431,437,438,441,450,456,457,473,500,506,511,514,519,521,522,523,524,525,526,527,528,529,532,533,535,538,542,550,552,553,554,555,556,558,566,577,578,579,581,587,588,589,600,604,611,613,639,647,649,664,670,671,672,684,700,705,709,710,711,721,742,758,767,778,780,782,784,787,800,807,809,811,819,822,825,829,833,844,849,855,866,867,868,869,873,876,877,880,881,882,883,884,885,886,887,888,889,900,902,905,911,939,950);
    
    //var mobile = document.forms["homeform"]["mobile"].value;  //  (305)-123-4567
    var code   = mobile.substring(1, 4);  //  305
    console.log("CODE: " + code);
    var code_npa = mobile.substring(1,4);  // 000 - 200
    console.log("CODE_NPA: " + code_npa);
    var area_code = parseInt(code_npa);
    console.log("AREA_CODE: " + area_code);
    var nxx_n = mobile.substring(6,7) ;   // 1
    console.log("NXX_N: " + nxx_n);
	var nxx_xx = mobile.substring(7,9) ;  // 23
	console.log("NXX_XX: " + nxx_xx);
	
    var length = AreaCode.length;  // array length
    for (var i = 0; i < length; i++) {
        if(AreaCode[i] == code ) { result = false; }
    }
	
	if ( area_code >= 0 && area_code <= 200 ) { result = false ;}
	if ( nxx_n == 1 ) { result = false ;}
	if ( nxx_xx == 11 ) { result = false ;}
		

    return result;
}

function validateDOB(MM, DD, YYYY) {
    var result = false;
    var day    = DD;
    var month  = MM;
    var year   = YYYY;
    var age    = 18;
    var mydate = new Date();
    mydate.setFullYear(year, month-1, day);

    var currdate = new Date();
    var setDate  = new Date();        
   setDate.setFullYear(mydate.getFullYear() + age,month-1, day);

    if ((currdate - setDate) > 0){
    // above 18
    console.log("Above 18");  // testing, check console
    result = true;
    } else {
    console.log("Below 18");
     result = false;
    }    
    
return result;   

}


// 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);
    // See Number.prototype on line 315
    var area_range = area1.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 ( area1 !== 666 && area1 !== 000 && area_range == false && area2 !== 00 && area3 !== 0000 ) {
        result = true;
    }
    return result;
}


// 
Number.prototype.between = function (min, max) {
    return this > min && this < max;
};

// ADD THIS TO WORDPRESS:

/* ============================================================
   THEME STYLES
=============================================================*/
function theme_styles() {
        wp_enqueue_style('alert_css', get_template_directory_uri()  . '/css/sweetalert.css' );
}
add_action ('wp_enqueue_scripts', 'theme_styles');


function theme_js() {
	wp_enqueue_script ('alert_js', get_template_directory_uri() . '/js/sweetalert.min.js', array('jquery'),'', true  ) ;
}
add_action('wp_enqueue_scripts', 'theme_js') ;