jQuery Validate (Date Range)

后端 未结 6 676
长发绾君心
长发绾君心 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:48

    I've never used the validation plugin, but a look through the API suggests that something like this might work:

    $.validator.addMethod('daterange', function(value, element) {
        if (this.optional(element)) {
            return true;
        }
    
        var startDate = Date.parse('2010-11-29'),
            endDate = Date.parse('2010-12-15'),
            enteredDate = Date.parse(value);
    
        if (isNan(enteredDate)) {
            return false;
        }
    
        return ((startDate <= enteredDate) && (enteredDate <= endDate));
    });
    

    You would then, I think, need to add the daterange class the the appropriate element.

提交回复
热议问题