jQuery 按 ID 和表单组验证

jQuery Validate by ID and form group

我需要通过id验证而不是输入名称,因为我输入的名称是data[name]。我还有一条规则允许一个或两个字段。

我可以添加带有 id 的规则,但我不能合并这两个规则。有办法吗

<input id="email" type="email" class="form-control contact-method" name="data[email]" placeholder="Email">
<input id="phone" type="text" class="form-control contact-method" name="data[phone]" placeholder="Phone">

JS代码:

$('#requestDemo').validate({  // <- attach '.validate()' to your form
    debug: false,
    errorElement: "span",
    errorClass: "help-block",
    rules: {
        phoneInput: {
            require_from_group: [1, ".contact-method"]
        },
        emailInput: {
            require_from_group: [1, ".contact-method"]
        }
    },

我认为您面临的问题是 name 中的特殊字符,您可以在对象键等情况下使用字符串文字

jQuery(function($) {
  $('#requestDemo').validate({ // <- attach '.validate()' to your form
    debug: true,
    errorElement: "span",
    errorClass: "help-block",
    rules: {
      'data[email]': {
        require_from_group: [1, ".contact-method"]
      },
      'data[phone]': {
        require_from_group: [1, ".contact-method"]
      }
    }
  })
});
<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>

<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.css">
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.js"></script>

<form id="requestDemo" method="post" action="">
  <input id="email" type="email" class="form-control contact-method" name="data[email]" placeholder="Email">
  <input id="phone" type="text" class="form-control contact-method" name="data[phone]" placeholder="Phone">

  <input type="submit" class="button" value="Submit" />
</form>

I also have a rule to allow one or both fields.

当您未在 HTML 标记中的任何位置使用 contact-method class 时,我看不出您的 require_from_group 规则将如何运作。您需要添加此 class...

<input id="email" type="email" class="form-control contact-method" name="data[email]" placeholder="Email">
<input id="phone" type="text" class="form-control contact-method" name="data[phone]" placeholder="Phone">

I need to validate by id and not input name because my input name is data[name]

有两种解决方法...

  1. When the name contains special characters like dots or brackets, you would simply surround the name with quotes...

    jQuery(function($) {
        $('#requestDemo').validate({
            ....
            rules: {
               "data[email]": {
                    require_from_group: [1, ".contact-method"]
                },
                ....
    

    演示 1:http://jsfiddle.net/umgjLvhd/


  1. 使用 the .rules('add') method 而不是 .validate()...

    声明规则
    $('.contact-method').each(function() {
        $(this).rules('add', {
            require_from_group: [1, $(this)]
        });
    });
    

    演示 2:http://jsfiddle.net/mcotx2oh/


注意:无论您如何声明规则,input 元素 必须 仍然包含唯一的 name属性。