【问题标题】:jquery validation plugin- change border color + no textjquery 验证插件 - 更改边框颜色 + 无文本
【发布时间】:2013-03-25 01:14:16
【问题描述】:

无论如何我可以在错误时将边框颜色更改为红色而不是删除文本吗?这几乎是我想要显示的唯一错误,一旦输入/文本区域成为焦点,它就会消失。

<div id="contact-form">
            <form method="get" id="contacts">
                <div class="contact-controls">
                    <label class="contact-label" for="name-input">NAME</label>
                    <div class="input-wrapper">
                        <input type="text" name="contact-name" id="name-input" placeholder="Your Name" required>
                    </div>
                </div>
                <div class="contact-controls">
                    <label class="contact-label" for="email-input">EMAIL</label>
                    <div class="input-wrapper">
                        <input type="text" name="contact-email" id="email-input" placeholder="Your Email" required>
                    </div>
                </div>
                <div class="contact-controls">
                    <label class="contact-label" for="message-input">MESSAGE</label>
                    <div class="input-wrapper">
                        <textarea name="contact-message" id="message-input" rows="15" placeholder="Hi there&#33;" required></textarea>
                    </div>
                </div>
                <div class="contact-controls">
                    <input type="hidden" name="save" value="contact">  
                    <button type="submit" class="contact-button">Send</button>  
                </div>
            </form>
        </div>

【问题讨论】:

  • 我们需要查看您当前正在“删除文本”的验证脚本
  • 啊,我还没有,我想知道有没有办法,或者这是另一个可以做我正在寻找的插件
  • 是的,有一个简单的方法。看我的回答。

标签: jquery jquery-validate


【解决方案1】:

执行以下操作:

1) 将errorPlacement 回调函数设置为return false 以抑制错误消息文本。

2) 修改插件的 .error.valid class 的 CSS 以在元素周围显示彩色边框或其他内容。默认情况下,插件已经自动应用和删除这两个class,因此没有为此设置特殊的插件选项。

工作演示:http://jsfiddle.net/8vQFd/

jQuery

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        // your rules and options,
        errorPlacement: function(){
            return false;  // suppresses error message text
        }
    });

});

【讨论】:

  • 甜,这非常完美。问题:在页面加载时,我的第一个输入字段已经显示错误(.error {border: 1px red solid});我怎样才能使它只在用户输入不正确的字符时才标记错误?
  • @vytfla,只需按照我的 jsfiddle 操作即可。因为我看不到你的.validate() 函数,所以我不确定这是怎么回事。你能修改我的 jsFiddle 来显示这个问题吗?
  • @vytfla,这只是my original jsFiddle,加载时不会突出显示任何错误。您必须单击“更新”或“分叉”按钮来保存任何更改。
  • @vytfla,所以只需从元素中删除 class="error"。出现错误时,插件会自动将其放回原处。也可以在 JS 中查看我的 cmets:jsfiddle.net/RaPUY/2
  • 谢谢,我以为我把它拿出来了。我不明白你的 cmets-#myform -> #myForm 你为什么要在最后添加评论斜线?
【解决方案2】:

没有 Jquery 没有添加其他/修改任何功能只需添加 Css

input[class="error"]{border:1px solid #f00 !important;} input[class="form control error"]{border:1px solid #f00 !important;} .error p{color:#f00 !important;}

【讨论】:

    【解决方案3】:

    您可以定义一个名为 .input-validation-error 的 CSS 类,当验证规则被破坏时,它会通过 jQuery 验证插件添加到输入元素中。

    .input-validation-error {
        border: solid 1px #ff0000;
    }

    【讨论】:

      【解决方案4】:

      一个非常简单的基于 css 的方法如下所示:

      input.error{border: 1px solid red;}
      

      您可能希望对此进行扩展,但这是一个很好的起点。

      【讨论】:

      • 与现有的看似相似的帖子相比,请更清楚地说明这篇帖子所提供的额外见解。相关的区别是什么?它有什么好处?收获这种好处的功能差异是什么?
      • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      • 值得注意的是,与三年前的@Mohammed Sakhidel's answer 相比,它提供了类似的边框,但基于jQuery Validation 的特定input-validation-error 类或四年前的@user2650063's answer,目前尚不清楚,它为input.error 提供了一个选择器,但也提供了一些扩展选择器来解决其他情况。每个都涵盖了您的基本提案,仅使用 CSS。
      【解决方案5】:

      您可以在输入/文本区域中添加一个类来添加红色边框

      我称之为.error

      input:focus,textarea:focus{
      outline: none !important;
      }
      
      input.error, textarea.error{
      outline:red groove thin;
      }
      

      然后使用 jquery 去查找类为 '.error' 的输入 /textareas 并将其值设置为空。

      $(document).ready(function() {
        $('#contact-form').find('.error').val(' ');
      });
      

      Jsfiddle V1

      更新

      JS 示例

       $(document).ready(function() {
        $('#contact-form').find('.error').val(' ');
          $('input, textarea').click(function() {
          $(this).removeClass('error').val('');
         });
      
          var name = $('#name-input').val();
      
          if(name == ''){
            $('#name-input').addClass('error');   
          }
         });
      

      CSS

       input:focus,textarea:focus{
        border-color: initial;
       }
      
       input.error, textarea.error{
        border:1px solid red;
       }
      

      HTML

      <div id="contact-form">
              <form method="get" id="contacts">
                  <div class="contact-controls">
                      <label class="contact-label" for="name-input">NAME</label>
                      <div class="input-wrapper">
                          <input type="text" name="contact-name" id="name-input" placeholder="Your Name" value="" class="" required>
                      </div>
                  </div>
                  <div class="contact-controls">
                      <label class="contact-label" for="email-input">EMAIL</label>
                      <div class="input-wrapper">
                          <input type="email" name="contact-email" id="email-input" placeholder="Your Email" value="" class="" required>
                      </div>
                  </div>
                  <div class="contact-controls">
                      <label class="contact-label" for="message-input">MESSAGE</label>
                      <div class="input-wrapper">
                          <textarea name="contact-message" id="message-input" rows="15" placeholder="Hi there&#33;" class="" required></textarea>
                      </div>
                  </div>
                  <div class="contact-controls">
                      <input type="hidden" name="save" value="contact">  
                      <button type="submit" class="contact-button">Send</button>  
                  </div>
              </form>
          </div>
      

      JSfiddle V2

      【讨论】:

      • 您的 jsfiddle 的电子邮件/消息边框已经是红色的;一旦用户在字段为空时按下发送,我正在寻找它们变为红色。
      • 如果出现错误,您使用哪种语言处理数据,将“错误”类添加到输入框中,(您也可以这样设置值)
      • 我认为你完全没有抓住重点。 OP 正在使用 jQuery Validate 插件,该插件已经具有各种内置回调选项来执行所有这些操作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 2012-01-27
      相关资源
      最近更新 更多