本文介绍了使用Jquery进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用jQuery来验证我的表单。我想看看用户名是否是一个单词。我在jquery中看到选择器,但不知道如何在我的情况下使用它。
I am using jQuery to validate my form. I would like to see if the username is a single word. I see selector in Jquery http://jqueryvalidation.org/category/selectors/ but don't know how to use it in my case.
<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>
推荐答案
- 函数):
- Add a validator(function) of this sort :
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!
});
现在在您的规则代码中:
now in your Rules code :
rules:{
"Username":{
required: true,
minlength:5,
word_check : true
},
这篇关于使用Jquery进行表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!