2016-07-06 67 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

是的,这是正确的。 –

相关问题