如何为每种错误类型指定自定义错误消息,例如required和date。我想要一个不同的消息,具体取决于错误类型。
我有一个看起来像这样的字段
<input type="text" id="dateOfBirth" name="dateOfBirth" class="required date" />
我的错误消息看起来像这样
This field is required
或Please enter a valid date
我怎样才能说出以下内容
Date of birth is required
和Please enter a valid date for Date of Birth
这就是我正在使用的
var validator = $("form:first").validate({
errorClass: 'Input_State_Error',
rules: {
dateOfBirth: "required date"
},
messages: {
dateOfBirth: "Please enter your date of birth"
},
// the errorPlacement has to take the table layout into account
errorPlacement: function(error, element) {
error.appendTo($('#ctl00_lblMessage'));
},
// specifying a submitHandler prevents the default submit, good for the demo
submitHandler: function() {
alert("submitted!");
},
// set this class to error-labels to indicate valid fields
success: function(label) {
// set as text for IE
label.html(" ").addClass("checked");
}
});
答案 0 :(得分:3)
关于验证插件的jquery date()
documentation,我想你建议:
var validator = $("form:first").validate({
...
messages: {
dateOfBirth: {
required: "Date of birth is required",
date: "Please enter a valid date for Date of Birth"
}
}
...
});