验证:发生错误时删除自定义成功消息

时间:2016-07-06 17:48:03

标签: javascript jquery jquery-plugins

我正在使用jQuery validate来使用远程调用来检查用户名是否存在。像这样:

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 个答案:

答案 0 :(得分: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();
        }
    }
}