这是我正在使用的代码:
$(document).ready(function() {
jQuery.validator.addMethod("val_address", function(value, element) {
return (value == 'Address only here...');
}, "Address must be set to a value other than default...");
jQuery.validator.addMethod("val_city", function(value, element) {
return (value == 'City only here...');
}, "City must be set to a value other than default...");
jQuery.validator.addMethod("val_zip", function(value, element) {
return (value == 'Zip only here...');
}, "Zip must be set to a value other than default...");
jQuery.validator.addMethod("val_state", function(value, element) {
return (value == 'State only here...');
}, "State must be set to a value other than default...");
$.validator.setDefaults({
submitHandler: function () { alert("submitted!"); }
});
var ruleSetaddress = {
val_address : true,
required: true
};
var ruleSetcity = {
val_city : true,
required: true
};
var ruleSetstate = {
val_state : true,
required: true
};
var ruleSetzip = {
val_zip : true,
required: true
};
$("#advanced-searchform").validate({
rules: {
search_address: ruleSetaddress,
search_city: ruleSetcity,
search_state: ruleSetstate,
search_zip: ruleSetzip
},
errorElement: "span" ,
messages: {
search_address: " Enter Address",
search_city: " Enter City",
search_state: " Enter State",
search_zip: " Enter Zip",
}
});
$("#searchform").validate({
rules: {
search_address: ruleSetaddress,
search_city: ruleSetcity,
search_state: ruleSetstate,
search_zip: ruleSetzip
},
errorElement: "span" ,
messages: {
search_address: " Enter Address",
search_city: " Enter City",
search_state: " Enter State",
search_zip: " Enter Zip",
}
});
}
您可以在此处查看表格--> http://dimitri.clientdemos.pw/
出于某种原因,什么都没有发生——使用 PHP 代码获得的值基本上显示为默认值。因此,规则是检查默认值是否未更改(不应该这样做)并检查是否正在发送空白值(同样不应该这样做)。
上面的代码有什么问题?
更新我现在可以看到一条警报消息 - 当我放一条随机警报消息时 - 但我怎么知道在哪里显示错误消息?如何定义这样的元素?在我上面的代码中
errorElement: "span" ,
这里的跨度是什么?它是文本框/跨度元素的 ID 吗?如何定义这样的错误元素?