具有日期验证的ASP.net MVC Datepicker

时间:2015-11-02 01:54:57

标签: jquery asp.net asp.net-mvc date datepicker

有谁能告诉我这段代码有什么问题?我正在尝试在允许用户输入时将日期选择日期上的日期验证为mm / dd / yyyy。我使用了建议给我的验证码,并在其中有一些其他代码,以确保弹出日历在鼠标按下或按Tab键时消失(如果他们根本没有输入日期)。

   $(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 < 1 || month > 12){
                  return false;
               }else if (day < 1 || day> 31){ 
                  return false;
               }else if ((month==4 || month==6 || month==9 || month==11) && day ==31){
                  return false;
               }else if (month == 2){
               var isLeapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
               if (day> 29 || (day ==29 && !isLeapYear)){
                  return false
               }
            }
           return true;
        });
    }

1 个答案:

答案 0 :(得分:0)

点击链接:

http://formvalidation.io/examples/jquery-ui-datepicker/

&#13;
&#13;
$(function() {

  $( "#jQueryValidateTest" ).validate();

  $( "[type=date]" ).datepicker({
    onClose: function() {
      $( this ).valid();
    }
  });
});
&#13;
.valid {
  background: green;
  color: #fff;
}
.error {
  background: red;
  color: #fff;
}
&#13;
<form id="jQueryValidateTest">
    <input type="date" required>
</form>​
&#13;
&#13;
&#13;