title: JavaScript Date Validation Function
body: <p>Can anyone explain how does the foll JS function validate date which needs to be of the form mm/dd/yyyy.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  function checkdate(input){
      var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
      var returnval=false
      if (!validformat.test(input.value))
          alert("Invalid Date Format. Please correct and submit again.")
      else{ //Detailed check for valid date ranges
          var monthfield=input.value.split("/")[0]
          var dayfield=input.value.split("/")[1]
          var yearfield=input.value.split("/")[2]
          var dayobj = new Date(yearfield, monthfield-1, dayfield)
          if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
              alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
          else
              returnval=true
      }
      if (returnval==false) input.select()
          return returnval
  }
  &lt;/script&gt;
  </code></pre>