2016-07-06 95 views
0

我正在使用jQuery驗證使用遠程調用來檢查用戶名是否存在。像這樣:驗證:發生錯誤時刪除自定義成功消息

remote: { 
    url: "MyUrl", 
    type: "GET", 
    complete: function (data) { 
     // Return success message if applicable 
     if (data.responseText == "true") 
     { 
      $("#divUserNameAvailable").show(); 
     } 
    } 
} 

但是,如果我留在現場並強制驗證錯誤,則消息將保留。

enter image description here

以下是完整的驗證:

if ($("#CreateAccountForm").length) { 
    $("#CreateAccountForm").validate({ 
     rules: { 
      UserName: { 
       required: true, 
       rangelength: [6, 20], 
       usernameFilter: true, 
       remote: { 
        url: "MyUrl", 
        type: "GET", 
        complete: function (data) { 
         // Return success message if applicable 
         if (data.responseText == "true") 
         { 
          $("#divUserNameAvailable").show(); 
         } 
        } 
       } 
      }, 
     }, 
     messages: { 

      UserName: { 
       remote: "This username is not available. Please enter a new username.", 
       required: "Please enter your username.", 
       rangelength: "Usernames must be between 6 and 20 characters in length and may not use any special characters such as \\/+ {{ }} [ ] | = , * ? ; : < > @." 
      }, 

     }, 

     errorPlacement: function (error, element) { 
      error.insertAfter(element); 
     }, 
     submitHandler: function (form) { 
      form.submit(); 
     } 
    }); 

我試圖隱藏在errorPlacement的DIV,但沒有任何工程。有任何想法嗎?

回答

1

如果它是一個成功的呼叫,然後隱藏錯誤<div>

remote: { 
    url: "MyUrl", 
    type: "GET", 
    complete: function (data) { 
     // Return success message if applicable 
     if (data.responseText == "true") 
     { 
      $("#divUserNameAvailable").show(); 
      // Hide the error div. Replace the `that_error_div_selector` with right selector. 
      $(that_error_div_selector).hide(); 
     } 
    } 
} 
+0

是的,但什麼其他的驗證(需要等)?我的問題是,如果它最初是成功的,那麼錯誤發生在現場後。 –

+0

@DaveBrock這是爲了那個特殊的驗證纔對嗎? –

+0

是的,這是正確的。 –

相關問題