jQuery验证规则和错误消息值检查

时间:2018-01-30 10:13:49

标签: javascript jquery jquery-validate

嗨我有显示错误信息的问题 这是我的验证

$("#fcontact").validate({
    rules: {
        phone01: {
            maxlength: 6
        },
        phone02: {
            maxlength: 6
        },
        phone03: {
            maxlength: 6
        },
        mobile01: {
            maxlength: 6
        },
        mobile02: {
            maxlength: 6
        },
        mobile03: {
            maxlength: 6
        },
        phone01: "Error",
        phone02: "Error",
        phone03: "Error",
        mobile01: "Error",
        mobile02: "Error",
        mobile03: "Error",
    },
});

和HTML

<form id="fcontact" name="fcontact" method="post">
    <div class="bdform">
      <div class="formbox">
        <table class="tblcontact">
          <tr>
            <td>
              <p class="blockphone"> 
                <label for="phone01">Mobilephone</label>
                <input style="ime-mode: inactive;" type="text" name="phone01" id="phone01" maxlength="6" value="<?php echo isset($_SESSION['contact']['phone01']) ? $_SESSION['contact']['phone01'] : '' ?>" class="input phone <?php echo isset($_SESSION['contact']['phone01']) ? '' : 'required' ?> onlynumber">
                -
                <input style="ime-mode: inactive;" type="text" name="phone02" id="phone02" maxlength="6" value="<?php echo isset($_SESSION['contact']['phone02']) ? $_SESSION['contact']['phone02'] : '' ?>" class="input phone <?php echo isset($_SESSION['contact']['phone02']) ? '' : 'required' ?> onlynumber">
                -
                <input style="ime-mode: inactive;" type="text" name="phone03" id="phone03" maxlength="6" value="<?php echo isset($_SESSION['contact']['phone03']) ? $_SESSION['contact']['phone03'] : '' ?>" class="input phone <?php echo isset($_SESSION['contact']['phone03']) ? '' : 'required' ?> onlynumber">
                </span>
              </p>
                <label for="mobile01">Telephone</label>
                <input style="ime-mode: inactive;" type="text" name="mobile01" id="mobile01" maxlength="6" value="<?php echo isset($_SESSION['contact']['mobile01']) ? $_SESSION['contact']['mobile01'] : '' ?>" class="input phone <?php echo isset($_SESSION['contact']['mobile01']) ? '' : 'required' ?> onlynumber">
                -
                <input style="ime-mode: inactive;" type="text" name="mobile02" id="mobile02" maxlength="6" value="<?php echo isset($_SESSION['contact']['mobile02']) ? $_SESSION['contact']['mobile02'] : '' ?>" class="input phone <?php echo isset($_SESSION['contact']['mobile02']) ? '' : 'required' ?> onlynumber">
                -
                <input style="ime-mode: inactive;" type="text" name="mobile03" id="mobile03" maxlength="6" value="<?php echo isset($_SESSION['contact']['mobile03']) ? $_SESSION['contact']['mobile03'] : '' ?>" class="input phone <?php echo isset($_SESSION['contact']['mobile03']) ? '' : 'required' ?> onlynumber">
                </span>
              </p>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <p class="center">
      <input type="submit" name="submit" id="submit" value="submit" class="btn btnsubmit hover" >
    </p>
  </form>

你可以看到结构,我使用jquery.validate.js。当电话或手机接收隐藏错误信息的值时,我想这样做,因为这里的代码在提交aria-required =&#34; true&#34;并且它显示错误消息,您可以在此处看到布局中显示6错误。当电话和手机都没有获得任何价值时,我可以将它变成一个错误显示吗?

1 个答案:

答案 0 :(得分:0)

如您所见,您已在rules对象中两次定义了相同的参数......

rules: {
    phone01: {
        maxlength: 6
    },
    phone02: {
        maxlength: 6
    },
    phone03: {
        maxlength: 6
    },
    mobile01: {
        maxlength: 6
    },
    mobile02: {
        maxlength: 6
    },
    mobile03: {
        maxlength: 6
    },
    phone01: "Error",  // <- 'phone01' is already defined above!
    phone02: "Error",  // <- 'phone02' is already defined above!
    phone03: "Error",  // <- 'phone03' is already defined above!
    mobile01: "Error", // <- 'mobile01' is already defined above!
    mobile02: "Error", // <- 'mobile02' is already defined above!
    mobile03: "Error", // <- 'mobile03' is already defined above!
},

如果您尝试设置自定义消息,那么这些消息会进入messages对象,rules的兄弟。

rules: {
    phone01: {
        maxlength: 6
    },
    phone02: {
        maxlength: 6
    },
    phone03: {
        maxlength: 6
    },
    mobile01: {
        maxlength: 6
    },
    mobile02: {
        maxlength: 6
    },
    mobile03: {
        maxlength: 6
    },
},
messages: {  // <- custom messages go inside of `messages`
    phone01: "Error", 
    phone02: "Error", 
    phone03: "Error",  
    mobile01: "Error", 
    mobile02: "Error", 
    mobile03: "Error", 
},