簡體   English   中英

在jquery驗證中使用遠程方法的問題

[英]Issue using remote method in jquery validation

我正在關注這篇文章以驗證我的表單

我的問題是當我必須使用remote方法時,例如remote: "check-username.php"

由於遠程方法的文檔對我來說不是那么清楚,我會知道:

我需要如何在PHP腳本中構建我的json輸出?

要使用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')
    }
}

你不需要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
                }
             }
         }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM