Eliminate error class in jQuery Validate once validation is successful

I'm having an issue with the jQuery Validate plugin.

Even after a field is successfully validated, the "error-message box" continues to be displayed.

How can I remove this box?

Here's 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) { 
            alert('valid form submitted'); 
            return false; 
        }
    });

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/

If you enter something in the first input field, it validates with a green border, but the red "error-box" remains visible.

Answer №1

When you achieve Victory, remember to eliminate the mistake attribute that was added to your error container section

Here is how your code should appear;

victory: function(label,element) {
    label.parent().removeClass('mistake');
    label.remove(); 
},

DEMO

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display or conceal a division underneath a dropdown menu based on selections retrieved from a SQL Server database

Presented here is my JavaScript code. function appendItemforPurchaseOrder() { debugger var rowNumber = parseInt($(".itemmapContainer").attr("data-rownumber")); rowNumber = isNaN(rowNumber) ? 1 : rowNumber + 1; var addNewItemDetailHtml = ...

Tips on correctly determining font size

I'm attempting to style a span element with a specific height (in this case, 23px). However, the browser is automatically adding some extra pixels to the element. There are no additional styles like padding or margin affecting it, just the size of th ...

Converting Dates with Ractive.js

Is there a way to transform the Epoch time value retrieved from a JSON endpoint into a readable time string format such as "Tue 19 Jan 11:14:07 SGT 2038" without relying on external libraries like moment.js? var ractive = new Ractive({ el: '#cont ...

Adding two input values using jQuery

Here is the function compute() that I am working with: function compute() { if ($('input[name=type]:checked').val() != undefined) { var a = $('input[name=service_price]').val(); var b = $('input[name=modem_pric ...

Unable to smoothly expand Div in React using Tailwind

I'm facing a challenge in animating a div that contains input elements. I want it to expand smoothly - initially, the text area is small and the other two inputs and buttons are hidden. When clicking on the text area, the input and button should be re ...

The CSS box-shadow property failed to render properly on Internet Explorer and Safari browsers

#keyboard { position: fixed; background: #eee; display: none; border: 1px solid #ccc; border-radius:7px; width: 950px; height: 300px; padding: 5px; cursor: move; background-image:url('BackgroundImage.jpg'); box-shadow: -5px -5px 5px 5px #888; -m ...

Angular 2 - Graphic Representation Tool for Visualizing Workflows and Processes - Resource Center

Looking for a library that can meet specific requirements related to diagram creation and rendering. We have explored jsplumbtoolkit, mermaid, and gojs, but none of these fully satisfy our needs. For example, we need the ability to dynamically change con ...

Automating the box selection process using table-react functionality

I am facing an issue with table-react. I need to implement a functionality where certain checkboxes should be checked based on user permissions. For instance, if the user has an id = 3 and can view companies with ids: 5, 6, 7, the checkboxes corresponding ...

Obtaining targeted information from JSON using JavaScript

Extracting specific data from a JSON object can be challenging, especially if you are new to JavaScript. Below is an example of JSON data containing various fields: {"Date":"2021-01-31........ to ....10.9,"windDir":"SSE"} ...

I will design a photo gallery on my website with distinct albums showcasing a multitude of captivating images

I am looking to develop a feature on my website that will allow users to create image galleries for each profile. Users should be able to upload multiple pictures and organize them into albums. However, I am unsure of how to approach building such function ...

Ways to ensure a div remains at the bottom of a page without using a wrapper

I am currently working on my website and this is the content I have in my body tag: #social-media { width: 175px; margin-left: auto; margin-right: auto; padding-top: 10%; } #social-media img { padding: 5px; } #soci ...

What is the best way to retrieve a property value from an object using the .find() method?

I've encountered a problem with the following code snippet in my function: let packName: string = respPack.find(a => {a.id == 'name_input'}).answer.replace(/ /,'_'); My goal is to locate an object by matching its id and retrie ...

What steps can be taken to confirm that a comment posted on a specific post is genuine and related to that post?

Currently, I am immersed in the world of AJAX, PHP, and MySQL. Below is a snippet of my PHP code: if(isset($_GET['postTextComment'])){ // The text of the comment $htpc = htmlspecialchars($_GET['postTextComment']); // ID of ...

Guide on how to bypass IDM when downloading a PDF file through a GET request

When I try to fetch a PDF by sending a GET request to the server and open it in a new tab, IDM interrupts my request and downloads the file instead. It changes the server response status to 204 and removes the headers. How can I prevent IDM from doing th ...

My jQuery AJAX request is not successfully communicating with my PHP script

I am not well-versed in AJAX (or jQuery), but I believed my task was fairly straightforward. However, when attempting to send an AJAX request using: $.ajax( requestObj ); it seems to fail, and I am seeking assistance. To provide context, here is how the ...

Yet again faced with an annoying gap between table rows and cells, for the 532nd instance

Once again, tables have defeated me. I am struggling with a simple table: <table style="width: 200px; margin: 20px auto; border-collapse: collapse; border-spacing: 0; border: none;"> <tr style="margin: 0; padding: 0; border: none; border-colla ...

Is it a wise decision to provide the client with a new token just one minute before the expiration of the old one?

When monitoring my backend, I constantly check the remaining time before the JWT expires, which is set to 15 minutes. If there is only one minute left or less, I generate a new JWT and include it in the response header as a setToken. The front-end can then ...

What is the best way to incorporate the .then method in a promise?

I'm currently working on some JS Express code that looks like this: app.get('/lists2', (req, res) => { mongo.getDB() .then(db => db.collection('dogs')) .then(collection => collection.find().toArray()) .then ...

PHP unable to parse data from Jquery.ajax PUT request

I'm currently working with the Slim PHP Framework and attempting to send FormData using Jquery.ajax() in the following manner: var formData = new FormData(); formData.append('some_name', 'some_data'); formData.append('a_file& ...

Animating jQuery Counter with Changing Background Color

I've implemented a Counter that counts from 0 to a specified number, and it seems to be working perfectly. However, I want to add an animation where the background color of a div height animates upwards corresponding to the percentage of the counter. ...