Kendo DatePicker fails validation for custom date format

前端 未结 6 1173
孤城傲影
孤城傲影 2021-02-02 11:48

I\'m using Kendo DatePicker to edit a Date field being displayed in a Kendo Grid in my ASP.NET MVC 4 project. In order to have the DatePicker being used for the Date field use c

6条回答
  •  一向
    一向 (楼主)
    2021-02-02 12:14

      [DataType(DataType.Date)]
      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
      [Required(ErrorMessage = "Pick a date from Kendo Date Picker")]
       public DateTime mydate{ get; set; }
    
        @(Html.Kendo().DatePickerFor(m=>m.mydate)
                    .Name("MyDate")
                    .Format("dd/MM/yyyy")
                     .ParseFormats(new string[] {"dd/MM/yyyy"}))
    

    /Add script document.ready function/

         $(document).ready(function () { kendo.culture("en-GB");
    
             $.validator.methods['date'] = function (value, element) {
        var check = false;
        var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
        if (re.test(value)) {
    
            var adata = value.split('/');
            var dd = parseInt(adata[0], 10);
    
            var mm = parseInt(adata[1], 10);
            var yyyy = parseInt(adata[2], 10);
            var xdata = new Date(yyyy, (mm - 1), dd);
    
            if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == (mm - 1)) && 
                (xdata.getDate() == dd)) {
    
                check = true;
            }
            else {
                alert(value);
                check = false;
            }
    
        } else
            check = false;
        return this.optional(element) || check;
    }
    

    });

提交回复
热议问题