【问题标题】:Falsifying an error with jQuery validate使用 jQuery validate 伪造错误
【发布时间】:2011-02-10 11:33:33
【问题描述】:
我有一个使用 jQuery 验证的表单。它工作正常,但是我有以下问题。
我有一个文本区域,它是必填字段。我有另一段代码在单击按钮(不是表单提交)时执行,它检查该字段是否已完成,如果已完成,则执行 Google Geocode 查找。如果找不到地址,我需要告诉 jQuery 验证该字段无效,并阻止提交表单。我尝试手动将 textarea 的类设置为“错误”并创建错误元素,即标签,但这并不妨碍提交表单。
各位有什么想法吗?
谢谢
铝
【问题讨论】:
标签:
jquery
jquery-validate
【解决方案1】:
试试这个:
$(document).ready(function(){
$.validator.addMethod("searchgeo", function(value, element) {
$.ajax({
type: "POST",
url: "http://"+location.host+"/searchgeo.php",
data: "address="+value,
dataType:"html",
success: function(msg)
{
// if the address exists, it returns a string "true"
if(msg == "true")
return false;
return true;
}
})
}, "invalid address");
$("#form").validate({
address: {
searchgeo: true
},
messages: {
address: {
searchgeo: "This address does not exists"
}
}
});