jQuery 的表单验证之单步自动验证

这次实现了,单步的自动难证,主要是运用了它的很多参数。这样基本就可以完成我们所需要的注册的验证了。

下面是我的码,只要按你的要求改一点点,就是完全你所想要的了:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>单步自动验证表单</title>
    <style type="text/css">
    .error{color:red;}
    </style>
   <script type="text/javascript" src="misc/js/jquery.min.js"></script>
   <script type="text/javascript" src="misc/js/jquery.form.js"></script>
   <script type="text/javascript" src="misc/js/jquery.validate.js"></script>
   <script type="text/javascript">
    $(function(){
    $("#validataForm").validate({
    rules: {
            firstname: "required",
            lastname: "required",
            username: {
                      required: true,
                      minlength: 8
                      },
            password: {
                      required: true,
                      minlength: 5
                      },
    confirm_password: {
                      required: true,
                      minlength: 5,
                      equalTo: "#password"
                      },
             email: {
                    required: true,
                    email: true
                     },
                   },
                  
                 messages: {
                 firstname: "Please enter your firstname",
                 lastname: "Please enter your lastname",
                 username: {
                 required: "Please enter a username",
                 minlength: "Your username must consist of at least 8 characters"
                 },
                 password: {
                 required: "Please provide a password",
                 minlength: "Your password must be at least 5 characters long"
                 },
                 confirm_password: {
                 required: "Please provide a password",
                 minlength: "Your password must be at least 5 characters long",
                 equalTo: "Please enter the same password as above"
                },
                 email: "Please enter a valid email address"
                 }
    });
    // check if confirm password is still valid after password changed
    $("#password").blur(function(){
    $("#confirm_password").valid();
    });
   
    });
   </script>
</head>
<body>
<form id="validataForm"  action="">
  <div>
    <label for="firstname" >firstname</label>
    <input type="text" name="firstname" />
    <label class="error" generated="true" for="firstname"></label>
  </div>
   <div>
    <label for="lastname" >lastname</label>
    <input type="text" name="lastname" />
    <label class="error" generated="true" for="lastname"></label>

  </div>
   
   <div>
    <label for="username" >username</label>
    <input type="text" name="username" />
     <label class="error" generated="true"  for="username"></label>
  
  </div>
   <div>
    <label for="password" >password</label>
    <input id="password"  type="password" name="password" />
    <label class="error" generated="true"  for="password"></label>
  </div>
   <div>
    <label for="confirm_password" >confirm_password</label>
    <input  id="confirm_password" type="password" name="password" />
    <label class="error" generated="true"  for="confirm_password"></label>
  </div>
 
    <div>
    <label for="email">email</label>
    <input type="text" name="email" />
    <label class="error" generated="true"  for="email"></label>
    </div>
  <div><input type="submit" /></div>
  </form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值