0

Im using a datepicker within a form and submitting the form using jquery ajax post.

I have a hidden field RequiredDate, and setting that in the change event for the datepicker

Im then using that in the ajax post

 var requiredDate = $("#RequiredDate").val();

The html for my hiden field is:

<input data-val="true" data-val-required="Please select the required date" id="RequiredDate" name="RequiredDate" type="hidden" value="" />

Im allowing hidden field validation using:

 $.validator.setDefaults({
        ignore: "#InternalColourId, #ExternalColourId, #RequiredDate"
    });

I want to be able to validate that a date has been picked (ideally without the use of 3rd party plugins)

Can you help?

Sparky
  • 98,165
  • 25
  • 199
  • 285
raklos
  • 28,027
  • 60
  • 183
  • 301

2 Answers2

0

Not the cleanest way, but this seems to work for me

  var isFormValid = form.valid();

        if (!$("#RequiredDate").val() > 0) {
            if (isFormValid)
            {
                $(".validation-summary-errors ul").empty();

            }
            if (!$(".date-invalid").length) {
                $(".validation-summary-errors ul").append("<li class='date-invalid'>Please Select required date</li>");
            }
            $(".validation-summary-errors").show();
            isFormValid = false;
        }
raklos
  • 28,027
  • 60
  • 183
  • 301
0

Quote OP:

"I'm allowing hidden field validation using:"

ignore: "#InternalColourId, #ExternalColourId, #RequiredDate"

No, you're not. You are doing the exact opposite of "allowing".

ignore: "#RequiredDate" means exactly what it says… this field is ignored during validation.

Since all hidden fields are ignored by default, you have to disable the ignore option like this…

ignore: []  // <- Set 'ignore' to nothing - allows validation of hidden fields

Alternatively, any other specific parameters inside ignore will also over-ride the default setting to ignore hidden fields. In other words, if you set ignore: ".ignore", then only fields with class ignore will be skipped.

See: jQuery Validate - Enable validation for hidden fields

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285