title: ASP.net MVC Datepicker with date validation
body: <p>Can anyone tell me what is wrong with this code? I am trying to validate the date on a datepicker date to mm/dd/yyyy when allowing user input. I used validation code that was suggested to me, and have some other code in there to make sure the popup calendar goes away when they mouse down or press the tab key (if they don't put a date in at all).</p>
  
  <pre><code>   $(function () {
          $('.datepicker2').datepicker()
         .on('changeDate', function (ev) {
             $('.datepicker2').datepicker('hide');
         });
         ForceDatePickerFormat();
      });
  
      function ForceDatePickerFormat(dateValue) {
          $('.datepicker2').on('keydown', function (e) {
              var keyCode = e.keyCode || e.which;
              if (keyCode == 9) {
                  $('.datepicker2').datepicker('hide');
              }
          });
          $(".datepicker2").on("blur", function (e) {
                 var selectedDate = dateValue;
                 if(selectedDate == '')
                 return false;
  
                 var regExp = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
                 var dateArray = selectedDate.match(regExp); // is format OK?
  
                 if (dateArray == null){
                   return false;
                 }
  
                 month = dateArray[1];
                 day= dateArray[3];
                 year = dateArray[5];        
  
                 if (month &lt; 1 || month &gt; 12){
                    return false;
                 }else if (day &lt; 1 || day&gt; 31){ 
                    return false;
                 }else if ((month==4 || month==6 || month==9 || month==11) &amp;&amp; day ==31){
                    return false;
                 }else if (month == 2){
                 var isLeapYear = (year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0));
                 if (day&gt; 29 || (day ==29 &amp;&amp; !isLeapYear)){
                    return false
                 }
              }
             return true;
          });
      }
  </code></pre>