This is the code you have always wanted: Validating date, time, time-range the user has entered before the form is submitted & without refreshing the form. On the form, you can have your date/time field as a text field or as a date/time field.
Simply put the following code in your JSHeader & call the appropriate function with argument.
To validate date-range, use validateTimeRange() in similar fashion.
function validateDate(textDate) { /* check/detect the separator used in date */ var dateSeparator="/"; if(textDate.indexOf(dateSeparator)==-1) dateSeparator="-"; var beginSep=textDate.indexOf(dateSeparator); //No separator found, not valid format if(beginSep==-1) return false; var month=textDate.substring(0, beginSep); //check for second separator var lastSep=textDate.indexOf(dateSeparator, beginSep+1); if(lastSep==-1) return false; var date=textDate.substring(beginSep+1, lastSep); var year=textDate.substring(lastSep+1); //We'll force the user to enter 4 digit year if(year.length !=4) return false; var thedate=new Date(parseInt(year), month-1, date); var yearD=thedate.getYear(); //Netscape behaves differently var theYear=document.layers ? yearD+1900 : yearD; if(thedate.getMonth() != month-1 || thedate.getDate() != date || theYear != year) return false; //success, format is valid, return true return true; } function validateTime(textTime) { var timeSeparator=":"; var sepIndex=textTime.indexOf(":");
Requires Free Membership to View
Register today to access targeted resources from our editorial writers and independent industry experts focused on Lotus Domino, Notes, Workplace and other related technologies.
if (sepIndex==-1) return false; //extract hours & minutes var hrs=textTime.substring(0, sepIndex); var mins=textTime.substring(sepIndex+1, textTime.length); //check whether they are numeric if(isNaN(parseInt(hrs)) || isNaN(parseInt(mins)) ) return false; if(parseInt(hrs) < 0 || parseInt(hrs) > 23) return false; if(parseInt(mins) < 0 || parseInt(mins) > 59) return false; return true } function validateTimeRange(textTime) { //time separated by "-" (dash), e.g. 10:30-14:15 var timeSeparator=":"; var dts=textTime.split(","); for(i=0; i < dts.length; i++) { var dtIn=dts[i].split("-"); for(k=0; k < dtIn.length; k++) { if(validateTime(dtIn[k])==false) return false; } } }
This was first published in March 2001