我无法找到类似的解决方案来解决我在SO上遇到的问题。我正在使用jQuery验证插件来验证表单。我添加了一些自定义验证器方法来验证下拉列表,另一个验证文本框。下拉验证工作正常,但文本框验证只“部分”工作。我说'部分',因为实际的验证部分工作,但应该显示的错误信息不会显示。有问题的字段是id3文本字段,并且它与javascript关联。将自定义验证器添加到jquery验证插件不显示错误消息
这是我的html:
<form class="form-horizontal" name="paymentInformation" id="paymentInformation" action="verifyOrder.cfm" method="post" role="form">
<div class="form-group">
<fieldset class="col-sm-12">
<!-- Row 1 -->
<div class="row">
<div class="col-sm-6">
<label for="booktype" class="col-6 col-form-label">
Book Type *
</label>
<select class="custom-select col-4" id="booktype" name="booktype">
<option selected value="">Select book type</option>
<option value="val1">E-BOOK</option>
</select>
</div>
<div class="col-sm-6">
<label for="id2" class="col-6 col-form-label">
Number
</label>
<input type="form-control" placeholder="Number" type="text" id="id2" name="id2" class="col-10">
</div>
</div>
<!-- /Row 1 -->
<!-- Row 2 -->
<div class="row">
<div class="col-sm-6">
<label for="firstname" class="col-6 col-form-label">
First Name *
</label>
<input type="form-control" type="text" id="firstname" name="firstname" class="col-12" placeholder="Name">
</div>
<div class="col-sm-6">
<label for="id2" class="col-6 col-form-label">
Book Name
</label>
<input type="form-control" placeholder="Book Name" type="text" id="id3" name="id3" class="col-10">
</div>
</div>
<!-- /Row 2 -->
<div class="row">
<div class="col-sm-6">
<label for="lastname" class="col-6 col-form-label">
Last Name *
</label>
<input type="form-control" type="text" id="lastname" name="lastname" class="col-12" placeholder="Name">
</div>
</div>
<!-- /Row 2 -->
<label for="description" class="col-10 col-form-label">
Country description
</label>
<textarea id="description" name="description" rows="4" class="form-control txtarea-rounded"></textarea>
<div class="form-check">
<label class="col-sm-6">Countries: </label>
<br />
<label class="form-check-label col-10">
<input class="form-check-input col-10" type="checkbox" name="usa" value="Y">USA
<br />
<input class="form-check-input col-10" type="checkbox" name="uk" value="Y"> UK
</label>
</div>
</fieldset>
</div>
<div class="hideDiv">
<input type="submit" name=btnSubmit value="PROCEED TO THE NEXT STEP " class="blueButton">
</div>
</form>
下面是我的javascript:(问题自定义规则在CAPS评论)
var authorlist = [{"AUTHOR":"DONNA
EDWARDS","COUNTRY":"USA","REGION":"MIDWEST"},{"AUTHOR":"EMERALD
JONES","COUNTRY":"UK","REGION":"EU"},
{"AUTHOR":"SHAKESPEARE","COUNTRY":"UK","REGION":"EU"}];
function checkName(){
var nameIsValid = true;
var nametocheck = $("#id3").val();
$.each(authorlist, function(index, val){
//console.log(val.AUTHOR);
if(val.AUTHOR.toUpperCase() == nametocheck.toUpperCase()){
//console.log(val.AUTHOR);
nameIsValid = false;
return false;
}
});
return nameIsValid;
}
$("#id3").on("blur", function(){
if(!checkName()){
//display error: This Book name already exists!
}
else{
//remove error message
}
console.log("The name entered is valid: " + checkName());
});
function checkForm() {
var formIsValid = checkName() && $("#paymentInformation").valid();
if (formIsValid) {
$(".hideDiv").show();
} else {
$(".hideDiv").hide();
}
}
$("#booktype").on("change", function() {
checkForm();
});
$("#paymentInformation").on("keyup", function() {
checkForm();
});
// first custom rule
$.validator.addMethod("valueNotEquals", function(value, element, arg) {
return arg !== value;
}, "Value must not equal arg.");
//2ND CUSTOM RULE - THIS RULE PARTIALLY WORKS, BUT ERROR DOES NOT DISPLAY
$.validator.addMethod("booknameExists", function(value,element,arg){
}, "Book name must not pre-exist");
$.validator.setDefaults({
errorElement: "span",
errorClass: "help-block",
highlight: function(element) {
$(element).parent().removeClass('has-success').addClass('has-error');
},
unhighlight: function(element) {
$(element).parent().removeClass('has-error').addClass('has-success');
},
errorPlacement: function(error, element) {
if (element.parent('.input-group').length || element.prop('type') ===
'checkbox' || element.prop('type') === 'radio') {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
$("#paymentInformation").validate({
rules: {
'booktype': {
valueNotEquals: ""
},
'firstname': {
required: true,
maxlength: 200
},
'lastname': {
required: true,
maxlength: 200
},
'id3': {
required: true,
maxlength: 100,
booknameExists: false
}
},
messages: {
'booktype': {
valueNotEquals: "Select a book type.",
},
'firstname': {
required: "Enter your First Name.",
maxlength: "Your First Name cannot exceed 200 characters"
},
'lastname': {
required: "Enter your Last Name.",
maxlength: "Your Last Name cannot exceed 200 characters"
},
//THE FIRST 2 STANDARD BUILT IN ERROR MESSAGES ARE DISPLAYED CORRECTLY,
//BUT THE LAST CUSTOM ERROR MESSAGE - booknameExists - DOES NOT
'id3': {
required: "Book name is required.",
maxlength: "Book name cannot exceed 200 characters",
booknameExists: "Book name already exists!"
}
}
});
CSS是在拨弄但完整起见,在这里它是:
.hideDiv {
display: none;
}
.has-error {
color: red;
}
这里是链接到小提琴:https://jsfiddle.net/0puehapu/
任何帮助表示赞赏!
我不明白你在做什么这样做。如果你打算使用jQuery Validate插件,那么你不需要编写所有的手动验证函数,这些函数会在模糊和添加/删除消息时触发......插件会自动处理所有这些乏味的事情。 – Sparky
@Sparky - 我相信我需要这个自定义验证器的以下情况:我需要验证在文本字段中输入的值(我们称之为textVal),通过循环json对象并确保textVal不存在json对象。如果textVal已经存在于json对象中,那么我需要用一个错误信息来标记它,让用户知道由于textVal已经存在,他们需要输入一个不同的值。据我所知,jquery验证插件可以检查所需的,最小长度,最大长度等,但类似上面的东西必须是自定义验证。我错了吗? –
我想你可能会误解插件应该做什么。它根据各种事件触发的规则检查字段,自动应用类,显示消息等。当规则尚不存在时创建自定义方法......就像您需要使某个字段的值符合现有规则未涵盖的某些条件一样;例如,来自不明国家的手机号码格式。即使如此,该插件仍然会自动处理显示/隐藏消息和应用类。 – Sparky