使用Jquery进行表单验证

时间:2014-01-21 12:12:30

标签: jquery html forms validation

我正在使用jQuery来验证我的表单。我想看看用户名是否只是一个单词。我在Jquery http://jqueryvalidation.org/category/selectors/中看到了选择器,但在我的情况下不知道如何使用它。

<form id="register" name="register" action="/codejudge/contest.php" method="post" >
<label for ="Username"> Username </label><br>
<input type="text" class="register-control" id="Username" name="Username"  placeholder="Enter Username"> <br><br>
<label for ="Password"> Password </label><br>
<input type="password" class="register-control" id="password" name="password" placeholder="Enter Password" ><br><br>
<label for ="Confirm-Password"> Confirm Password </label><br>
<input type="password" class="register-control" id="Confirm_Password" name="Confirm_Password" placeholder="Confirm Password" ><br><br>
<label for="email" > Email </label><br>
<input type ="email" class="register-control" id="email" name="email" placeholder="Enter Valid Email"><br><br>
<button type="submit" >Submit</button>

</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#register").validate({
errorElement: 'div',
rules:{
    "Username":{
    required: true,     
    minlength:5
    },
"password":{
required:true,
minlength:5
},
"Confirm_Password":{
required:true,
equalTo: "#password"
},
"email":{
required:true,  
}
},
messages: {
    Username:{
                 required: "Please provide a username",
                minlength: "Your password must be at least 5 characters long"
    },
     password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
                },
     Confirm_Password: {
                required: "Please provide a confirm password",
                equalTo: "Please enter the same password as above"
                }
    },
    email:{
                required: "Please provide a valid email",
    } 
  });  
 });
 </script>

3 个答案:

答案 0 :(得分:5)

  1. 添加此类验证器(函数):
  2. jQuery.validator.addMethod('word_check', function (value, element) { 
      var word = "ausername";
      var space_check = word.split(" ")[1];
      //if space check is undefined you have a single word!
    });
    

    现在在您的规则代码中:

    rules:{
        "Username":{
        required: true,     
        minlength:5,
        word_check : true
        },
    

答案 1 :(得分:4)

在字段上添加正则表达式匹配,以确保它只是字母,数字, - 或_以下是如何向字段添加rege匹配。

$("#Textbox").rules("add", { regex: "^([a-zA-Z0-9-_])*$" })

答案 2 :(得分:2)

您可以创建一个自定义规则来检查一定数量的单词。例如

$("#register").validate({
jQuery.validator.addMethod("amountOfWords", function(value, element, options) {
    return this.optional(element) || /*regular expression logic*/;
}, "Too many words entered");

rules:{
    "Username":{
    required: true,     
    minlength:5,
    amountOfWords: 1
    }

}
}