9

I am using the jQuery Validate plugin, and I've encountered a problem.

Whenever someone writes something in the field, and the field gets validated with no errors, the "error-message box" is still shown.

My question is, how can I remove this box?

This is my code:

CSS:

.register-box .field .has-error{
    border: 1px solid red !important;

}
.register-box .field .has-error.success {
    border:none !important;
}
.register-box .field .has-success{
    border: 1px solid #42CDA1 !important;
    background: #EEFBF7 !important;
    color: black !important;
    border-bottom: 0px !important;
}
.register-box .success > .error{
    border: 1px solid #42CDA1 !important;
}
.register-box .field .valid{
    border: 1px solid #42CDA1 !important;
}
.register-box .error{
    margin-left: 200px !important;
    float: left;
    width: 150px;
    max-width: 150px;
    text-align: left;
    color: red;
    background: red;
    color: white;
    font-weight: bold;
    font-size: 11px;
    -webkit-font-smoothing: antialiased;
    font-smooth: always;
    -moz-osx-font-smoothing: grayscale;
    padding: 5px 7px;

}

jQuery:

  $('#myform').validate({

            debug: true,
            errorClass: "has-error",
            errorElement: "div",
            errorContainer: $("#warning, #summary"),

            errorPlacement: function (error, element) {
                        var name = $(element).attr("name");
                        error.appendTo($("#" + name + "_validate"));
                        $("#" + name + "_validate").addClass('error');
                    },


            /*errorPlacement: function(error, element) {
                error.appendTo("#errorHolder");
            },*/
            success: function(label,element) {
                label.hide();
                //var parent = $('.success').parent().get(0); // This would be the <a>'s parent <li>.
                //$(parent).addClass('has-success');    
            },

            rules: {
                username: {
                     required: true
                },
                email: {
                    required: true,
                    minlength: 5
                }

        },

            highlight: function(element, errorClass) {
                    $(element).addClass(errorClass);
                },

        messages: {
                firstname: "Enter your firstname",
                lastname: "Enter your lastname",
                username: {
                    required: "Enter a username",
                    minlength: jQuery.format("Enter at least {0} characters"),
                    remote: jQuery.format("{0} is already in use")
                }
            },
        submitHandler: function (form) { // for demo
            alert('valid form submitted'); // for demo
            return false; // for demo
        }
    });

HTML:

<div class='register-box'>
  <form  id="myform" class="register-form">
    <?php 
        if($checksuccess){ alert("success",$success); }
        if($checkerror){ alert("error",$error);
        }
    ?>
    <div class="field">
      <label for="username">Username</label>
      <div id="username_validate"></div>
      <input type="text" name="username"  value="" placeholder="Enter your desired username"/>
    </div>
    <div class="field">
      <label for="email">E-mail</label>
      <div id="email_validate"></div>
      <input type="email" name="email" value="" placeholder="Enter a valid e-mail address"/>
    </div>
    <button class="login-btn" data-disable-with="Signing In..." name="login" type="submit">Sign In</button>
  </form>
</div>

Please see this jsFiddle for an example: http://jsfiddle.net/5eb3pctr/

As you can see, if you write something in the first input field, it gets validated (green border around input) - although the red "error-box" is visible.

2
  • At first glance, I noticed that you have addClass(errorClass) function, but you don't have removeClass(errorClass) when the validation is OK. Commented Aug 14, 2014 at 10:14
  • 2
    You don't need success or highlight as you're not doing anything different than the default. By default, the plugin will apply and remove the error class and show/hide the label. BTW- the error class is removed with unhighlight, not success. Commented Aug 14, 2014 at 14:17

1 Answer 1

22

In Success you need to remove error class which you have added in your error container div

So your code will look like this;

success: function(label,element) {
    label.parent().removeClass('error');
    label.remove(); 
},

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.