Everything About JavaScript Date Validation

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

    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.

    By submitting your registration information to SearchDomino.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchDomino.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.