Date Validation
From All n One's bxp software Wixi
JavaScript Date Validation script
- autoFill
- originalPlus
- strFieldName
- strFieldCustom
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;
}
}
function fn_CheckDOBFormat( strField ){
var objField = document.getElementById(strField);
var aryDOBInForm = objField.value.split('/');
var strErrorsFound = 'None';
if(aryDOBInForm.length != 3){
strErrorsFound = 'Date Of Birth entered is not in the format DD/MM/YYYY';
}
if(aryDOBInForm[1] > 12){
strErrorsFound = 'Date Of Birth entered is not in the format DD/MM/YYYY';
}
if(aryDOBInForm[0].length != 2){
strErrorsFound = 'Date Of Birth entered is not in the format DD/MM/YYYY';
}
if(aryDOBInForm[1].length != 2){
strErrorsFound = 'Date Of Birth entered is not in the format DD/MM/YYYY';
}
if(aryDOBInForm[2].length != 4){
strErrorsFound = 'Date Of Birth entered is not in the format DD/MM/YYYY';
}
if(strErrorsFound != 'None'){
alert(strErrorsFound);
objField.value = '';
objField.setAttribute('placeholder','Please enter a date in the format DD/MM/YYYY');
}
}