Manage Learn to apply best practices and optimize your operations.

Everything About JavaScript Date Validation

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

Dig Deeper on Domino Resources - Part 4

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.




  • iSeries tutorials's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...