问题
I am taking input of date of birth in dd/mm/yyyy format and using the datepicker plugin
<input type="text" id="birth_date" name="birth_date" value="" class="form-control date">
And jQuery for date class is
$('.date').datepicker({
todayBtn: "linked",
keyboardNavigation: false,
forceParse: false,
calendarWeeks: true,
autoclose: true,
format: 'dd/mm/yyyy'
});
When I validate input field using jQuery Validate it gives me error "Please enter a valid date." for only dd/mm/yyyy format, instead it is working fine for mm/dd/yyyy format
$("#frm").validate({
rules: {
first_name: { required: true },
date_of_birth: { required: true }
},
messages: {
first_name: { required: 'Please enter first name' },
date_of_birth: { required: 'Please select the date of birth' }
},
errorPlacement: function (error, element) {
error.insertAfter(element);
},
submitHandler: function(form) {
form.submit();
}
});
Why I am getting this error as I am only validating for required field?
回答1:
"Why I am getting this error as I am only validating for required field?"
The jQuery Validate plugin can pick up rules in several ways, including, but not limited to...
- Declared within the
rulesobject of the.validate()method. - HTML5 inline attributes, such as
required="required". - Class names, such as
class="required".
When you use class="date" on the input field, you are invoking the date rule within the jQuery Validate plugin. Remove the date class from the input. If you depend on the date class for a visual style, then you must change the name of this class to something else.
来源:https://stackoverflow.com/questions/54745453/wrong-required-validation-for-datepicker-input-field