0

I have a form with a date field. The field is required and should not allow a date sooner than 2 days from the current date.

<input type="date" id="DayDate" name="DayDate" 
    class="form-control day-date input-validation-error" min="2023-05-09" 
    placeholder="Date" data-val="true" data-val-required="Date is required." required />

<span class="text-danger field-validation-error" data-valmsg-for="DayDate"
    data-valmsg-replace="true"></span>

The calendar appears to format correctly and disables all days preceding 2023-05-09.

Selecting any date that is enabled outputs a validation error of Please enter a value greater than or equal to NaN.

I tried adding the following rule in jQuery:

    $("#DayDate").rules("add",
        {
            required: true,
            min: "2023-05-09",
            messages: {
                required: "Required",
                min: "2 Day Minimum"
            }
        });
    });

Even after the above code, the control's rules.min value says NaN.

Can't the min attribute be used for an input type=date control? I'm missing a detail, but I don't see it.

3
  • The min rule is working as designed. Refer to the documentation. min only takes a number, hence the NaN (Not a Number) error. jqueryvalidation.org/min-method If you want a rule that evaluates dates then you'll need to write a custom method for that. Commented May 8, 2023 at 23:17
  • Using min, I can disable the days preceding the min set date. Though validation isn't working and I understand what you're saying. So how can a custom method disable dates in the dropdown UI? For example, minDate set to 2023-05-18 and all days prior to that date are disabled? Commented May 10, 2023 at 0:43
  • You're mixing HTML5 validation attributes and elements with jQuery Validate plugin. The HTML5 attributes/elements do not translate exactly how you want, or how you expect, over to jQuery Validate. If you want to use HTML5, then use HTML5 all by itself. Otherwise, change it into a type="text" field along with a custom jQuery Validate method. Commented May 10, 2023 at 1:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.