如何显示 jQuery 验证中的错误?

发布于 2024-09-24 21:09:53 字数 105 浏览 0 评论 0原文

我们是否有任何函数可以在验证表单时返回所有错误消息?

我尝试使用 defaultshowerros() 函数,但它返回当前正在验证的元素的错误消息。如何获取整个表单的所有错误信息?

Do we have any function which returns all the error messages while validating a form?

I have tried using the defaultshowerros() function but it returns the error message for the element it is currently validating. How can I get all the error messages of the whole form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走走停停 2024-10-01 21:09:53

如果您存储对验证器的引用,例如:

var validator = $("form").validate();

您可以随时调用 .errors().invalidElements() ,例如:

var errors = validator.errors(); //get the error elements, the actual labels
var errors = validator.invalidElements(); //the invalid elements themselves

如果您'不是真的在错误之后,只是希望它们出现在一个地方,请使用内置的 errorLabelContainerwrapper 选项,例如:

<ul id="errors"></ul>

并引用:

$("form").validate({ errorLabelContainer: "#errors", wrapper: "li" });

并且您的错误将全部显示在该列表中,如果存在,该列表也会自动显示/隐藏/没有任何错误。

If you store a reference to the validator, for example:

var validator = $("form").validate();

You can call .errors() or .invalidElements() at any time on it, for example:

var errors = validator.errors(); //get the error elements, the actual labels
var errors = validator.invalidElements(); //the invalid elements themselves

If you're not really after the errors and just want them to appear in once place, use the built-in errorLabelContainer and wrapper options, for example:

<ul id="errors"></ul>

And reference that:

$("form").validate({ errorLabelContainer: "#errors", wrapper: "li" });

And your errors would appear all in that list, which is also automatically shown/hidden if there are/aren't any errors.

扬花落满肩 2024-10-01 21:09:53

验证插件应在错误所在字段旁边显示错误。您的输入框使用 id 吗?如果是这样,也请使用名称,并在规则和消息中为 jquery 提供名称属性的值。希望这有帮助。

The validation plugin should show an error beside the field where the error is. Are you using id's for your input boxes? If so use a name as well and give jquery the value of the name attribute in your rules and messages. Hope this helps.

放赐 2024-10-01 21:09:53

虽然迟到了,但我发现您还可以使用 invalidHandler() 函数实例化 validate() 对象:

var $jqvForm = $(".jqvForm").validate({
    invalidHandler: function(e, validation){
        console.log("invalidHandler : event", e);
        console.log("invalidHandler : validation", validation);
    }
});

validation 变量包含一个带有表单项及其错误消息的变量invalid(对象)。

Late to the party, but I found you can also instantiate the validate() object with a invalidHandler() function:

var $jqvForm = $(".jqvForm").validate({
    invalidHandler: function(e, validation){
        console.log("invalidHandler : event", e);
        console.log("invalidHandler : validation", validation);
    }
});

The validation variable contains a variable invalid (object) with form items and their error messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文