Wrong required validation for datepicker input field

回眸只為那壹抹淺笑 提交于 2019-12-13 05:47:34

问题


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 rules object 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!