jQuery验证 [英] jquery validate
问题描述
我有这个jquery代码,使用输入文本表单中的参数加载远程页面
I've this jquery code, loading a remote page with a parameter from an input text form
$(document).ready(function() {
$('form').submit(function() {
$('#result').load("/api.php", {
'url': $("input:text[name=url]").val()
});
});
});
我需要验证输入文本,即url.
I need to validate the input text, that is a url.
如何在提交之前使用Validate插件进行验证?
How can I use the Validate plugin to do validation before the submit?
推荐答案
在您的PHP页面中,只需返回true
或错误消息,例如"This site isn't allowed"
(请注意区别,没有引用true
).
In your PHP page just return true
or an error message, e.g. "This site isn't allowed"
(note the difference, true
isn't quoted).
然后使用 remote
规则,如下所示:
Then use the remote
rule, like this:
$("form").validate({
rules: {
url: {
remote: "/api.php"
}
}
});
默认情况下,它将发送一对查询,元素的名称(已经是url
)及其值,即您之前传递的值.字符串被视为错误消息,而true
被视为通过验证.
By default it'll send one query pair, the element's name (already url
) and it's value, exactly what you were passing before. A string is treated as the error message and true
is treated as passing validation.
值得注意的是,此规则很特殊,它将在验证完成之前等待AJAX请求完成,因此它们都在同一个存储桶中,而不是单独的验证过程/步骤.
It's worth noting this rule is special, it'll wait on the AJAX request to finish before validation completes, so it's all in the same bucket, not a separate validation process/step.
这篇关于jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!