我使用jQuery來驗證我的表單。我想看看用戶名是否是一個單詞。我看到Jquery http://jqueryvalidation.org/category/selectors/中的選擇器,但不知道如何在我的情況下使用它。使用jquery的表單驗證
<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>
var word =「ausername」;不應該是「用戶名」? – chandan111
這只是我使用的自定義字符串。從邏輯上講,是的。用戶名將出現在該變量中。 Goodluck – Makrand
我寫了'jQuery.validator.addMethod('word_check',函數(值,元素){ var word =「用戶名」; var space_check = word.split(「」)[1]; return(typeof space_check =='undefined') //如果空格檢查未定義,您只有一個單詞! },「不允許使用特殊字符/空格」); 'form not subimited當我輸入用戶名空間但它不顯示錯誤 – chandan111