jQuery Validate (Date Range)

后端 未结 6 703
长发绾君心
长发绾君心 2020-12-14 03:47

Im using the jQuery validate plugin and was wondering if there was a way to validate if the date entered into a field was a date like yyyy-mm-dd AND the the date falls betwe

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 04:31

    If you want a reusable function, you might extend Emily and lonesomeday's answers to allow an argument to be provided:

    $.validator.addMethod('daterange', function(value, element, arg) {
         // Same as above
    
         var startDate = Date.parse(arg[0]),
             endDate = Date.parse(arg[1]),
             enteredDate = Date.parse(value);       
         // Same as below
    
     }, $.validator.format("Please specify a date between {0} and {1}."))
    

    See the source of the jQuery validation range method for an example.

提交回复
热议问题