ErrorPlacement in jQuery validation is causing an issue where errors cannot be removed even after they have been corrected

Can you assist me in resolving an issue? I am struggling with removing an error message once a user has corrected their mistake, particularly when it comes to validating an email field. The error keeps getting added multiple times until validation is successful. Here is the code snippet:

$( "#bookings" ).validate({
      rules: {
        email: {
          required: true,
          email: true
        },
      message: {
        email: {
          required: "field is required",
          email: "invalid email"
        },
      errorLabelContainer: $("ul", $('div#results')), wrapper: 'li',

        errorContainer: $('div#results'),

        errorPlacement: function(error, element) {
            $('#results').append(error);
        }
      success: function(error){ 
          error.remove(); 
      }

});



<div id="results">

</div>
<form name="bookings" id="bookings" method="post" action="#">
    <div class="item" style="clear:left;">
        <label>E Mail *</label>
        <input id="femail" name="email" type="text">
    </div>
</form>

I want the error message to appear only once. I have succeeded in removing the error, but it still gets appended repeatedly until the validation is accepted.

Answer №1

Give this a try:- http://jsfiddle.net/adiioo7/hGNRG/

JS:-

$("#bookings").validate({
    rules: {
        email: {
            required: true,
            email: true
        },
        message: {
            email: {
                required: "field is necessary",
                email: "invalid email"
            },
            errorLabelContainer: $("ul", $('div#results')),
            wrapper: 'li',
            errorContainer: $('div#results'),

            errorPlacement: function (error, element) {
                $('#results').append(error);
            },
            success: function (error) {
                error.remove();
            }

        }
    }
});

Answer №2

Eliminate the errorLabelContainer, wrapper, errorContainer, errorPlacement, and success options and replace them with this:


  wrapper: 'li',
  errorLabelContainer: $('#results')

The previous settings essentially duplicate (poorly) the default behavior of the Validation plugin.

View a working example here: http://jsfiddle.net/hGNRG/1/ (credit to @wiz-kid for creating the fiddle).

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

Adjust the size of the plane in Three.js to match the entire view

English is not my strong suit, as I am Japanese. I apologize for any confusion. Currently, my focus is on studying Three.js. I aim to position a Plane directly in front of the camera as the background. My goal is to have the Plane background fill the en ...

Steps for uploading files using the shortcut $.post method in jQuery

I've been searching for syntax examples, but haven't found any that seem to fit my needs. My code is simple and works well. When I upload data, the response message displays in a specific div on my page. $("#my_form").submit(function(e ...

Exploring the contrast between window and document within jQuery

I'm curious about the distinction between document and window in jQuery. These two are commonly utilized, but I haven't quite grasped their differences. ...

Image remains fluid within a static div without resizing

Any assistance would be greatly appreciated. I am currently facing an issue with a fixed div that is floating at the bottom of the screen, serving as ad space for the mobile version of a website. The problem arises when attempting to resize the browser win ...

Verify the MAC address as the user types

I need to verify a form field for MAC Addresses and have implemented the following code that does the job. $('body').on('keyup', '#macAddess', function(e){ var e = $(this).val(); var r = /([a-f0-9]{2})([a-f0-9]{2})/i, ...

Utilizing Office.js: Incorporating Angular CLI to Call a Function in a Generated Function-File

After using angular-cli to create a new project, I integrated ng-office-ui-fabric and its dependencies. I included in index.html, added polyfills to angular.json, and everything seemed to be working smoothly. When testing the add-in in Word, the taskpane ...

"The power of Node JS in handling JSON data and gracefully

I'm having trouble extracting a specific part of a JSON object in Node JS. When I print the response body, the entire object is displayed correctly. However, when I try to access object.subsonic-response, it returns NaN. I've spent a lot of time ...

Utilizing a custom filter for object manipulation within Vuetify's v-autocomplete

When using vuetify v-autocomplete with an object where the object consists of Key-Value pairs, the search functionality is based on the item-text. In my example, I am displaying the object as {1200-Chloride Systems}. Is it possible to make the search funct ...

Ways to avoid single-sided border from overlapping with border radius

When adding a 2px bottom border to a text field with a 4px corner radius on the container, the border tends to curl around the edge due to the larger radius. I am looking for a solution to keep the bottom edge flat. [What I DON'T want: border wrappin ...

I am looking to concatenate the existing URL with the language segment using jQuery

Looking to update the URL when clicking on a language name? Simply add the current path after the language section. For example, change the URL https://example.com/de_DE/about-us/ to https://example.com/us_EN/about-us/ and vice versa. I attempted this code ...

I'm looking to incorporate a hover effect on a hyperlink within JEditorPane utilizing Swing in Java. How can I achieve this

After successfully implementing the hyperlink in jEditorPane through this link, I now aim to incorporate a hover effect to the hyperlink. I attempted to apply CSS using the following code: MyHTMLEditorKit kit = new MyHTMLEditorKit(); setEditorKitForConten ...

Stop text from being selected across all browsers in Firefox

My current CSS includes: *{ -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } If you visit http://jsfiddle.net/KyF5x/ and click be ...

if statement for a method within the ng-click directive

Whenever I click the button, I want to either execute createRole() if RoleName is not set or EditRole() method if RoleName is set. However, for some reason, EditRole() is being executed for both cases. <button type="submit" ng-click="selectedItem.Rol ...

ways to incorporate searching within JSON data using AJAX and jQuery in JavaScript

My search box needs to have JSON data appended into it. Below is the HTML code: <input type="search" id="merchantName" name="merchant" placeholder="enter merchant name"></input> I have JSON data containing merchant names that I want to appen ...

What is the most effective way to display a tooltip next to an image?

I've been exploring various jQuery plugins in search of a tooltip feature that displays text, description, and rating while using the same image inside the tooltip. However, I have not been successful in finding one that fits my criteria. Therefore, ...

Divide the web service response into multiple parts

i am utilizing a web service to populate HTML tags. [WebMethod] public string GetHelloMessage(string country) { string example = "<div>amin</div>"; return example; } and using jQuery: url: ServiceUrl, type: "POST", contentType: "applic ...

Arranging an array of objects based on a specific keyword and its corresponding value

I have an array of objects that looks like this: [ { "type": "Exam", "value": 27 }, { "type": "Lesson", "value": 17 }, { "type": "Lesson", &qu ...

Error: The function styles$1.makeStyles is not defined

Currently, I am experimenting with rollup for bundling. However, when I run the code, I encounter the following problem: https://i.stack.imgur.com/8xLT3.png import { makeStyles } from '@material-ui/styles'; const styles = makeStyles({ subHead ...

JQuery is displaying HTML code instead of the expected value

I'm currently facing a JQuery issue. When we input test% in the textfield and call a function upon clicking the save button, something unexpected happens. The form data is serialized and sent via AJAX to the server side, but it's being received a ...

Issues with HTML Email display

Hey there, I am facing an issue while trying to send an HTML email. I am aware that Outlook does not support a lot of CSS formatting, but I can't figure out why this simple setup is breaking. Is there a quick fix or alternative solution? Take a look a ...