2014-02-24 135 views
7

我正在使用jQuery驗證插件(http://jqueryvalidation.org/)進行表單驗證。有一個我想做遠程驗證的字段。jQuery驗證遠程錯誤消息

示例代碼:

$("#myform").validate({ 
    rules: { 
     email: { 
      required: true, 
      email: true, 
      remote: "check-email.php" 
     } 
    } 
}); 

當其無效時,它顯示出 「請修正該字段」。如何獲得遠程驗證的自定義錯誤消息,例如「已使用的電子郵件地址,請使用其他電子郵件。」?

謝謝。

回答

4

他們的文檔似乎顯示message選項。這可以幫助嗎?

http://jqueryvalidation.org/validate

例如在其網站上:

$(".selector").validate({ 
    rules: { 
    name: "required", 
    email: { 
     required: true, 
     email: true 
    } 
    }, 
    messages: { 
    name: "Please specify your name", 
    email: { 
     required: "We need your email address to contact you", 
     email: "Your email address must be in the format of [email protected]" 
    } 
    } 
}); 
0

我都面臨着同樣的問題,我發現這樣的解決方案。

$(document).ready(function() { 
 
    $("#myform").validate({ 
 
     rules: { 
 
      email: { 
 
       required: true, 
 
       email: true, 
 
       remote:"check-email.php" 
 
       } 
 
      }, 
 
     messages: { 
 
      email:"please fix this field" 
 
      } 
 
    }); 
 
});

嘗試這種

2

只需傳遞消息的數組作爲第二個參數到validate()函數。要指定 用於遠程驗證的自定義錯誤消息,請使用如下所示的密鑰遠程。

$("#myform").validate({ 
    rules: { 
    email: { 
     required: true, 
     email: true, 
     remote: "check-email.php" 
    } 
    }, 
    messages: { 
    email: { 
     required: "This field is required", 
     email: "Invalid Email Address", 
     remote: "Email address already in use. Please use other email." 
    } 
    } 
}); 
+0

非常感謝您的回答。但它似乎不起作用。它是否可以不再使用最新版本(版本1.11.1)? – user1995781

+0

@ user1995781你是否從你的「check-email.php」文件發送了對失敗驗證的響應? –

+0

@ user1995781您需要從「check-email.php」文件發送「true」驗證成功的響應,驗證失敗則發送「null」或「false」。響應應該採用JSON格式。 –

4

這裏是你code.php解決

rules: { 
    shopname: 
      { 
    required: true, 
    remote:"code.php", 
      }, 
     } 

現在文件 -

如果輸入就可以了,然後顯示NOTHING

如果輸入不正確,那麼你必須顯示引述文本喜歡 -

回聲「‘這名被黃牌警告,你應該嘗試VIVEK98779797’」;

所以現在這段代碼可以通過PHP 顯示自定義錯誤消息使用遠程方法

0

消息:{ 遠程: 「有些不對勁」}

/* **服務器端應該返回「false」或「true」,字符串類型,而不是布爾值。 */