简体   繁体   English

如何使用jquery.validation插件不将error元素显示为标签

[英]How to not display error element as label with jquery.validation plugin

Okay guys, 好的伙计们,

I have read through all the other posts and question on jQuery Validation plugin and they don't seem to have what I'm looking to do. 我已经阅读了关于jQuery Validation插件的所有其他帖子和问题,他们似乎没有我想要做的事情。

I would like to have the display error not show with message but just create a red border around the input field instead. 我希望显示错误不显示消息,而只是在输入字段周围创建一个红色边框。

Here is just some of the form: 这里只是一些形式:

<form id="donate_form" name="checkoutForm" method="post">
    <label class="desc" id="title0" for="billing_name_first">
    Name
    <span id="req_1" class="req">*</span>
</label>
<div>
    <span class="left">
        <input name="billing.name.first" id="billing_name_first" type="text" class="field text required" value="" tabindex="1" />
        <label for="billing_name_first">First</label>
    </span>
    <span class="right">
        <input name="billing.name.last" id="billing_name_last" type="text" class="field text required" value="" tabindex="2" />
        <label for="billing_name_last">Last</label>
    </span>
</div>

I am assuming that I need to place the class required on the input?? 我假设我需要在输入上放置所需的类?

Then with CSS hide the label.error which is spit out by the plugin? 然后使用CSS隐藏由插件吐出的label.error I've tried that but no go. 我试过了,但没有去。

Am I looking in the right place? 我在正确的地方寻找吗?

Thanks! 谢谢!

All these solutions seem to be more complicated than they need to be. 所有这些解决方案似乎都比他们需要的更复杂。 Here's a simple solution. 这是一个简单的解决方案。 .error is added to invalid fields by default. 默认情况下, .error会添加到无效字段中。 So, first thing we need to do is style it so it changes the border and background to different shades of red: 所以,我们需要做的第一件事是将它设置为样式,以便将边框和背景更改为不同的红色阴影:

.error {
    border-color: #cd0a0a !important;
    background: #fef1ec !important;
}

Then, in your JavaScript: 然后,在您的JavaScript中:

$(function() {
    $("#donate_form").validate({
        errorPlacement: $.noop
    });
});

The errorPlacement option is how the validate plugin lets you override how the error message is placed. errorPlacement选项是validate插件允许您覆盖错误消息的放置方式。 In this case, we simply make it do nothing, and thus no error message is created. 在这种情况下,我们只是让它什么都不做,因此没有创建错误消息。

I understand what you are trying to achieve; 我理解你想要实现的目标; I had the same requirements but had serious difficulties trying to achieve it, but I did. 我有相同的要求,但在尝试实现它时遇到了严重的困难,但我做到了。 Here's how: 这是如何做:

  1. I created two CSS style classes "errorHighlight" and "textinput", like so: 我创建了两个CSS样式类“errorHighlight”和“textinput”,如下所示:

    .errorHighlight { border: 2px solid #CC0000; } .errorHighlight { border: 2px solid #CC0000; } .textinput {border: 1px silver solid;} .errorHighlight { border: 2px solid #CC0000; } .textinput {border: 1px silver solid;}

  2. At design time, I applied the "textinput" class to my text input fields: <input type="text" class="textinput" /> 在设计时,我将“textinput”类应用于我的文本输入字段: <input type="text" class="textinput" />

  3. And then in my jQuery form validation plugin settings inside my Javascript file, I added the following general validation settings for all forms on this page: 然后在我的Javascript文件中的jQuery表单验证插件设置中,我在此页面上为所有表单添加了以下常规验证设置:

      $.validator.setDefaults({ errorPlacement: function(error, element) { $(element).attr({"title": error.text()}); }, highlight: function(element){ //$(element).css({"border": "2px solid #CC0000"}); $(element).removeClass("textinput"); $(element).addClass("errorHighlight"); }, unhighlight: function(element){ //$(element).css({"border": "2px solid #CC0000"}); $(element).removeClass("errorHighlight"); $(element).addClass("textinput"); } }); 

Now, whenever a field fails validation, the " highlight " callback function is called on the element. 现在,只要字段验证失败,就会在元素上调用“ highlight ”回调函数。 The " errorPlacement " callback function prevents the default action of inserting a label after the erring input field, instead it appends the error message to the " title " attribute of the field (which can be used as the source of text for a tooltip display). errorPlacement ”回调函数阻止在错误输入字段之后插入标签的默认操作,而是将错误消息附加到字段的“ title ”属性(可用作工具提示显示的文本源) 。

Hope this helps. 希望这可以帮助。

You can extract just the error message in errorPlacement: and append it to whatever you like, like this: 您可以在errorPlacement中仅提取错误消息:并将其附加到您喜欢的任何内容,如下所示:

errorPlacement: function(error, element) {
    $('#error-div').html(error[0].innerHTML)//<-error[0].innerHTML is the error msg.
} 

Try: 尝试:

$(function() {
    $("#donate_form").validate({
        errorPlacement: $.noop
    });
});

I don't know if this is the correct way to do it, but we use: 我不知道这是否是正确的方法,但我们使用:

jQuery.validator.messages.required = '';

That suppresses the error messages and leaves the input border. 这会抑制错误消息并离开输入边框。

Actually, the jQuery Plugin documentation points to an example that does this, so I guess it's the accepted method.. 实际上, jQuery插件文档指出了一个这样做的例子,所以我想这是可接受的方法..

Customized message display: No messages displayed for the required method, only for type-errors (like wrong email format); 自定义消息显示:没有为所需方法显示消息,仅针对类型错误(如错误的电子邮件格式); A summary is displayed at the top ("You missed 12 fields. They have been highlighted below.") 摘要显示在顶部(“您错过了12个字段。它们已在下面突出显示。”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM