如何在jQuery-validate中指定有效字段

时间:2019-10-25 10:50:53

标签: javascript jquery jquery-validate

我有2个字段:姓名查询和出生日期,而姓名查询是必填项,而DoB不是。我有以下表单,用于在“名称查询”字段下显示验证消息:

var myForm = $("#myform").validate({
  rules: {
    NameQuery: "required",
    DoBQuery: {
      required: false
    }
  },
  messages: {
    NameQuery: "Please fill in name query field",
    DoBQuery: ""
  }
});
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>


<form id="myform" method="post" action="">
  <div>
    <label for="NameQuery">Name</label>
    <input type="text" id="NameQuery" name="NameQuery">
  </div>
  <div>
    <label for="DoBQuery">Date of Birth</label>
    <input type="text" id="DoBQuery" name="DoBQuery">
  </div>
  <input type="submit" value="Search">
</form>

现在,我想为“名称查询”字段验证消息添加一些样式。在代码段中添加了一些代码,请注意,<div class="error__input"></div>必须像代码段中一样在两个字段中都存在

var myForm = $("#myform").validate({
  rules: {
    NameQuery: "required",
    DoBQuery: {
      required: false
    }
  },
  messages: {
    NameQuery: "Please fill in name query field",
    DoBQuery: ""
  },
  errorElement : 'div',
  errorLabelContainer: '.error__input'
});
.error__input{
  min-height: 20px;
}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>
<form id="myform" method="post" action="">
  <div>
    <label for="NameQuery">Name</label>
    <input type="text" id="NameQuery" name="NameQuery">
    <div class="error__input"></div>
  </div>
  <div>
    <label for="DoBQuery">Date of Birth</label>
    <input type="text" id="DoBQuery" name="DoBQuery">
    <div class="error__input"></div>
  </div>
  <input type="submit" value="Search">
</form>

现在两个字段中都存在验证消息,我知道jQuery-validate找到了error__input div并将其应用于该类,但是为什么它将消息添加到第二个字段中以及如何设置警报消息仅适用于第一条消息,而两个<div class="error__input"></div>都必须存在吗?

3 个答案:

答案 0 :(得分:1)

我想您想要这样的东西

var myForm = $("#myform").validate({
  rules: {
    NameQuery: "required",
    DoBQuery: {
      required: false
    }
  },
  messages: {
    NameQuery: "Please fill in name query field",
    DoBQuery: ""
  }
});
#myform .error {
    display: block;
    color: red;
}

#myform input {
  display: inline;
}

#myform > div {
  height: 50px;
}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>


<form id="myform" method="post" action="">
  <div>
    <label for="NameQuery">Name</label>
    <input type="text" id="NameQuery" name="NameQuery" style="display: inline-block;">
  </div>
  <div>
    <label for="DoBQuery">Date of Birth</label>
    <input type="text" id="DoBQuery" name="DoBQuery" style="display: inline-block;">
  </div>
  <input type="submit" value="Search">
</form>

答案 1 :(得分:1)

您可以使用errorPlacement将错误消息放在错误元素旁边。

errorPlacement: function(error,element){
   error.appendTo(element.next(".error__input"));
}

var myForm = $("#myform").validate({
  rules: {
    NameQuery: "required",
    DoBQuery: {
      required: false /* if it is not required, why you put here? You can simply not insert this input field in validate()'s rules. */
    }
  },
  messages: {
    NameQuery: "Please fill in name query field"
  },
  errorElement : 'div',
  errorPlacement: function(error,element){
    error.appendTo(element.next(".error__input"));
  }
});
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>
<form id="myform" method="post" action="">
  <div>
    <label for="NameQuery">Name</label>
    <input type="text" id="NameQuery" name="NameQuery">
    <div class="error__input"></div>
  </div>
  <div>
    <label for="DoBQuery">Date of Birth</label>
    <input type="text" id="DoBQuery" name="DoBQuery">
    <div class="error__input"></div>
  </div>
  <input type="submit" value="Search">
</form>

答案 2 :(得分:-1)

您应该在出生日期字段之后删除<div class="error__input"></div>标签