jQuery.Validate验证库的使用介绍(3)


$.metadata.setType("attr", "validate");


这样可以使用validate="{required:true}"的方式,或者class="required",但class="{required:true,minlength:5}"将不起作用 

例如,将上面示例代码改为:

复制代码 代码如下:


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="JavaScript" type="text/JavaScript" src="https://www.jb51.net/jquery-1.6.1.js"></script>
<script language="JavaScript" type="text/JavaScript" src="https://www.jb51.net/jquery.validate.min.js"></script>
<script language="JavaScript" type="text/JavaScript" src="https://www.jb51.net/jquery.metadata.js"></script>

<script>
$().ready(function() {
  $.metadata.setType("attr", "validate");
  $("#signupForm").validate();
});
</script>

<form method="get" action="">
    <p>
        <label for="firstname">Firstname</label>
    <input validate="{required:true}" />
 <p>
  <label for="email">E-Mail</label>
  <input validate="{required:true, email:true}" />
 </p>
 <p>
  <label for="password">Password</label>
  <input type="password" validate="{required:true,minlength:5}" />
 </p>
 <p>
  <label for="confirm_password">确认密码</label>
  <input type="password" validate="{required:true,minlength:5,equalTo:'#password'}" />
 </p>
    <p>
        <input type="submit" value="Submit"/>
    </p>
</form>


注意:规则部分应用完整形式,即

正确写法:

复制代码 代码如下:


<input validate="{required:true, email:true}" />


错误写法:

复制代码 代码如下:


<input validate="{required email}" />


2、将校验规则写到代码中

复制代码 代码如下:


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="JavaScript" type="text/JavaScript" src="https://www.jb51.net/jquery-1.6.1.js"></script>
<script language="JavaScript" type="text/JavaScript" src="https://www.jb51.net/jquery.validate.min.js"></script>
<script language="JavaScript" type="text/JavaScript" src="https://www.jb51.net/jquery.metadata.js"></script>
<script>
$().ready(function() {
  $("#signupForm").validate({
    rules:
    {
      firstname: "required",
      email:
      {
        required: true,
        email: true
      },
      password:
      {
        required: true,
        minlength: 5
      },
      confirm_password:
      {
        required: true,
        minlength: 5,
        equalTo: "#password"
      }
    },
    messages:
    {
      firstname: "请输入姓名",
      email:
      {
        required: "请输入Email地址",
        email: "请输入正确的email地址"
      },
      password:
      {
        required: "请输入密码",
        minlength: jQuery.format("密码不能小于{0}个字符")
      },
      confirm_password:
      {
        required: "请输入确认密码",
        minlength: "确认密码不能小于5个字符",
        equalTo: "两次输入密码不一致不一致"
      }
    }
  });
});
//messages处,如果某个控件没有message,将调用默认的信息
</script>
<form method="get" action="">
    <p>
        <label for="firstname">Firstname</label>
        <input />
    </p>
 <p>
  <label for="email">E-Mail</label>
  <input />
 </p>
 <p>
  <label for="password">Password</label>
  <input type="password" />
 </p>
 <p>
  <label for="confirm_password">确认密码</label>
  <input type="password" />
 </p>
    <p>
        <input type="submit" value="Submit"/>
    </p>
</form>


required:true 必须有值

required:"#aa:checked"id名称为aa的dom被选中时,则需要验证

required:function(){} 返回为真,表示需要验证(仅针对required有效,其它无效。)

后边两种常用于,表单中需要同时填或不填的元素

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdgfsg.html