Date Validation

Revision as of 18:33, 19 March 2014 by Philip Lacey (talk | contribs)
Revision as of 18:33, 19 March 2014 by Philip Lacey (talk | contribs)

JavaScript Date Validation script


function fn_ValidateDate ( autoFill, originalPlus, strFieldName, strFieldCustom ) {
	 
	var autoDate                                           = autoFill;
	var daysPassed                                         = originalPlus;
	var flag                                               ='False';
	var selectedDate                                       = document.getElementById(strFieldCustom).value;
	var dateToCheck                                        = new Date(document.getElementById(strFieldCustom).value);
	
	document.getElementById(strFieldName).value = selectedDate;
	
	var dayOfWeek                                          = dateToCheck.getDay();
	
	if (dayOfWeek == 6){
		if(autoFill == 0){
			alert("Date selected is a Saturday and is not allowed");
		}
		else{
			alert("Date auto selected for " + originalPlus + " days from today is a Saturday. This has now been changed to the following Monday");
			daysPassed                             = daysPassed + 2;
			flag                                   = 'True';
			TodayPlusXDays(daysPassed);	
		}
	}
	else if (dayOfWeek == 0){
		if(autoFill == 0){
			alert("Date selected is a Sunday and is not allowed");
		}
		else{
			alert("Date auto selected for " + originalPlus + " days from today is a Sunday. This has now been changed to the following Monday");
			daysPassed                             = daysPassed + 1;
			flag                                   = 'True';
			TodayPlusXDays(daysPassed);		
		}
	}
	for(var i=0; i <= IrishDates.length; i++){
		if(selectedDate == IrishDates[i]){
			if(autoFill == 0){
				alert("Date selected is a bank holiday in the Republic of Ireland and is not allowed");
			}
			else{
				alert("Date auto selected for " + originalPlus + " days from today is a bank holiday in the Republic of Ireland. This has now been changed to the following day");
				daysPassed                     = daysPassed + 1;
				flag                           = 'True';
				TodayPlusXDays(daysPassed);	
			}
		}
	}
	
	if((document.getElementById(strFieldCustom).value != ' ') && (flag == 'False')){
		var setDateOriginal                            = document.getElementById(strFieldCustom).value; 
		var Current_Date                               = setDateOriginal.split('/');
		var newDateToPopulate                          = Current_Date[1] + '/' + Current_Date[0] + '/' + Current_Date[2];
		document.getElementById(strFieldCustom).value  = newDateToPopulate;
		document.getElementById(strFieldName).value    = newDateToPopulate;	
	}	
}