2014-01-21 12 views
1

我使用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> 

回答

5
  1. 添加這類的驗證器(功能):
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 
    }, 
+0

var word =「ausername」;不應該是「用戶名」? – chandan111

+0

這只是我使用的自定義字符串。從邏輯上講,是的。用戶名將出現在該變量中。 Goodluck – Makrand

+0

我寫了'jQuery.validator.addMethod('word_check',函數(值,元素){ var word =「用戶名」; var space_check = word.split(「」)[1]; return(typeof space_check =='undefined') //如果空格檢查未定義,您只有一個單詞! },「不允許使用特殊字符/空格」); 'form not subimited當我輸入用戶名空間但它不顯示錯誤 – chandan111

4

添加一個正則表達式匹配的領域,以確保它是字母,數字, - 或_下面是如何將雷傑比賽增加一個字段。

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

您可以創建自定義檢查工作量的規則DS。例如

$("#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 
    } 

} 
}