title: jQuery date validation - format and range
body: <p>I have a requirement where I will have 2 date fields (<code>to</code>, <code>from</code>) and needs to validate them:</p>
  
  <ul>
  <li>whether they are mm/dd/yyyy format</li>
  <li>both the dates are in particular range </li>
  <li>as the name suggests <code>to</code> should always be greater than <code>from</code></li>
  <li>both fields become mandatory as soon as i enter any one of these</li>
  </ul>
  
  <p>what will be the best way to implement the same, am using jquery validation api</p>
  
  <p><strong>Note</strong>: I did read about format validation on some of the links like <a href="https://stackoverflow.com/questions/16544844/jquery-date-validation-yyyy-mm-dd">jQuery Date Validation (YYYY-MM-DD)</a> but these doesnt ensure that its a valid date I may end up entering something like <code>02/31/2999</code></p>
  
  <p>i tried </p>
  
  <pre><code>jQuery.validator.addMethod("DateFormat", function(value, element) {
      var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
      var values = value.split("/");
      return this.optional(element) || (value.test(date_regex) &amp;&amp; (new Date(values[2], values[0], values[1])));
  });
  </code></pre>