日期选择器输入字段所需的验证错误 [英] Wrong required validation for datepicker input field
问题描述
我正在以dd/mm/yyyy
格式输入出生日期,并使用datepicker插件
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">
日期类的jQuery是
And jQuery for date class is
$('.date').datepicker({
todayBtn: "linked",
keyboardNavigation: false,
forceParse: false,
calendarWeeks: true,
autoclose: true,
format: 'dd/mm/yyyy'
});
当我使用jQuery Validate验证输入字段时,出现错误请输入有效日期".仅适用于dd/mm/yyyy
格式,相反,它对于mm/dd/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();
}
});
为什么在仅验证required
字段时出现此错误?
Why I am getting this error as I am only validating for required
field?
推荐答案
为什么仅在必填字段中进行验证,却出现此错误?"
jQuery Validate插件可以多种方式选择规则,包括但不限于...
The jQuery Validate plugin can pick up rules in several ways, including, but not limited to...
- 在
.validate()
方法的rules
对象中声明. - HTML5内联属性,例如
required="required"
. - 类名,例如
class="required"
.
- Declared within the
rules
object of the.validate()
method. - HTML5 inline attributes, such as
required="required"
. - Class names, such as
class="required"
.
在input
字段上使用class="date"
时,您正在调用jQuery Validate插件中的date
规则.从input
中删除date
类.如果您依赖date
类提供视觉样式,则必须将此类的名称更改为其他名称.
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.
这篇关于日期选择器输入字段所需的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!