Extending jQuery UI components ( Overriding jQuery Datepicker to prevent wrong input)

前端 未结 2 1771
北海茫月
北海茫月 2021-02-20 02:34

I am trying to extend the jQuery UI Datepicker for adding some validations.

I searched a lot in the internet, but I could not get any help. I found SO q

2条回答
  •  遥遥无期
    2021-02-20 02:58

    Try this,

    JsFiddle

    $(function() {
        $( "#datepicker" ).datepicker({ dateFormat: "yy-M-dd" });
    
        $('#datepicker').on("keyup", function(e) {
    
        var val = this.value;
            if(!ValidateDate(val)){
            console.log("Date must be 2014-Mar-01 Format")
            }
    
    
    
    });
      });
    
    
    function ValidateDate(dtValue)
    {
        console.log(dtValue)
    var patt = new RegExp(/^20\d\d-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(0[1-9]|[12][0-9]|3[01])$/);
    var res = patt.test(dtValue);
    return res;
    }
    

提交回复
热议问题