我下面this文章,以驗證我的形式問題在jQuery驗證使用遠程方法
我的問題是,當我不得不使用remote
方法,例如remote: "check-username.php"
由於documentation遠程方法是不適合我那麼清楚,我想知道:
我需要如何構建我的JSON輸出在PHP腳本?
我下面this文章,以驗證我的形式問題在jQuery驗證使用遠程方法
我的問題是,當我不得不使用remote
方法,例如remote: "check-username.php"
由於documentation遠程方法是不適合我那麼清楚,我想知道:
我需要如何構建我的JSON輸出在PHP腳本?
你不需要JSON。你可以放任何文字。例如,您可以
echo "success";
爲通過了驗證,並輸入驗證信息,如:
echo "Username already exists.";
爲驗證失敗。
在回調做這樣的事情:
remote: {
url: "check-username.php" ,
type: "post" ,
data: {
username: function() {
return $("#username").val();
},
complete: function(data){
if(data.responseText != "success") {
alert(data.responseText);
//handle failed validation
}
}
}
要使用默認的方法remote
,遠程頁面必須與"true"
或"false"
一個字符串值響應。
有效(成功)必須爲:
echo "true";
無效(失敗):
echo "false"; // this can be any false. eg: 0 , "" , NULL.
的響應將作爲JSON來評價。
要應用錯誤消息,而不是默認值爲無效(「false」)的遠程響應,請將輸入名稱添加到驗證器選項messages
對象中,如下所示。
rules:{
username:{
remote: "check-username.php"
}
},
messages:{
username:{
remote: jQuery.format('{0} is already in use, please choose a different name')
}
}
謝謝@argonius。我實施了一點變化,而不是你的建議。我回歸真實,而不是成功。 – GVillani82 2013-04-25 12:29:19
如果字段名稱爲'用戶名',則不需要數據:{},但不需要「成功」。 – pgee70 2014-06-29 03:49:25
正確的答案是manta的,服務器應以「真」或「假」 – Magico 2015-04-18 14:35:02