• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 693
  • Last Modified:

Function to Check for Valid Dates

I am looking for a succinct (preferably using regular expressions as much as possible) to validate that two values passed into a function are dates that follow a "MM/DD/YYYY" format, and that the first date is less than or equal to the second date.

basically:

function checkDateRange(date1, date2) {
  // checks to see if date1 is a valid date in "MM/DD/YYYY" format
  // checks to see if date2 is a valid date in "MM/DD/YYYY" format
  // checks to see if date1 is less than date 2
}
0
DanielSKim
Asked:
DanielSKim
  • 2
2 Solutions
 
Pravin AsarCommented:
<script language="javascript">
function  isValidDate (inputDate) {
 
  if (inputDate.match (/^[0-1][0-9]\/[0-1][0-9]\/2[0-9][0-9][0-9]/)) {
     alert ('OKAY');
  }
  else {
        alert ('NO OKAY');
  }
}
function checkDateRange(date1, date2) {
   //checks to see if date1 is a valid date in "MM/DD/YYYY" format
   if (!isValidDate (date1)) { return false; }
   // checks to see if date2 is a valid date in "MM/DD/YYYY" format
   if (!isValidDate (date2)) { return false; }
   // checks to see if date1 is less than date 2
   var d1 = new Date (date1);
   var d2 = new Date (date2);
   if (d1 > d2) { alert ('Date 1 greater than Date 2'); }
}

</script>
<form>
<input type="text" name="date1" onBlur="isValidDate(this.value);">
<input type="text" name="date2" onBlur="isValidDate(this.value);">
<input type="button" value="Validate " onclick="checkDateRange(this.form.date1, this.form.date2);">
</form>
0
 
mvan01Commented:
Hi Daniel,

See this:  http://Q_20775188.html

Peace and joy.  mvan
0
 
Pravin AsarCommented:
Take this one, I forgot to add the return statements in isValidDate()


<script language="javascript">
function  isValidDate (inputDate) {
 
  if (inputDate.match (/^[0-1][0-9]\/[0-1][0-9]\/2[0-9][0-9][0-9]/)) {
     alert ('OKAY');
     return true;
  }
  else {
       alert ('NO OKAY');
       return false;
  }

}
function checkDateRange(date1, date2) {
   //checks to see if date1 is a valid date in "MM/DD/YYYY" format
   if (!isValidDate (date1)) { return false; }
   // checks to see if date2 is a valid date in "MM/DD/YYYY" format
   if (!isValidDate (date2)) { return false; }
   // checks to see if date1 is less than date 2
   var d1 = new Date (date1);
   var d2 = new Date (date2);
   if (d1 > d2) { alert ('Date 1 greater than Date 2'); }
}

</script>
<form>
<input type="text" name="date1" onBlur="isValidDate(this.value);">
<input type="text" name="date2" onBlur="isValidDate(this.value);">
<input type="button" value="Validate " onclick="checkDateRange(this.form.date1, this.form.date2);">
</form>
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now