当我尝试创建一个方法来使用additional-methods.js文件中提供的方法时,我得到了预期的验证。但是,如果用户输入了错误的值,而不是显示additional-methods.js
文件中定义的异常,则用户将看到当前元素的标题,该元素当前正用于类型提示。
我的JavaScript看起来像这样:
//setup input validation
validator = form.validate({
rules: {
city: {
required: true,
lettersonly: true
}
}
});
$('input, textarea').blur(function(){
element = $(this);
// if the user gave no value then show the this field is required
if(element.val() == element.attr('title')){
refreshError(element, 'This field is required');
}else if(element.val() == '') { //if the value is empty then check if it was required, also check if it had an input hint
//if the element is required display the error message
if(element.hasClass('required')){
refreshError(element, 'This field is required');
}
//if the title exists then we assume it is the input hint
if(element.attr('title') != ''){
element.val(element.attr('title'));
element.addClass('inputHint');
}
}else{
//if we got input validate it
response = element.valid();
//if we got an error clear the old one(conditional on existance), and display the new one. Otherwise clear any old error
if(!response){
this.defaultShowErrors();
}else{
errorId = element.attr('id')+'Error';
$('#'+errorId).remove();
}
}
});
表格的相关部分是:
<label for="city">City:</label>
<input type="text" name="city" id="city" class="required inputHint" title="Your city here" />
关于我做错了什么的任何想法?我使用的是jQuery Validator plugin
的1.9.0版本注意:我认为在看完addMethod() of jQuery validation plugin display error from title attribute之后我已经击中了钉子,但在尝试之后:
//setup input validation
validator = form.validate({
rules: {
city: {
required: true,
lettersonly: true
}
},
messages: {
city: {
lettersonly: "you must give a valid city"
}
}
});
我发现没有任何改变。
答案 0 :(得分:1)
我刚遇到此错误并遇到了ignoreTitle
选项:
//setup input validation
validator = form.validate({
ignoreTitle: true,
rules: {
city: {
required: true,
lettersonly: true
}
},
messages: {
city: {
lettersonly: "you must give a valid city"
}
}
});