Validation message does not seem to be disappearing on input

My form validates input fields, including textboxes and radio buttons. The validation error messages display correctly when trying to submit the form with incomplete fields. However, once a field has the required data, the validation error should disappear immediately. While the validation messages disappear on textboxes as the user types, they do not disappear for radio button selections.

This is how it appears before selecting:

https://i.sstatic.net/d8bys.png

And after a selection is made:

https://i.sstatic.net/oturt.png

My code is shown below:

<div class="form-group">
    <label class="col-lg-12 control-label label-input">
        Is this also your residence address?
        <a tabindex="-1" data-toggle="popover" data-placement="bottom" class="popover-dismiss" rel="popover" data-content="We may need to send some policy and claim documents to you via mail.">
            <img src="~/Images/svg/Icon Info.svg" height="16" width="16" />
        </a>
    </label>
    ... (additional HTML code) ...
</div>

@Html.ValidationMessageFor(m => m.Address.RiskIsResidence, null, new { @class = "col-lg-12" })

Here is the CSS used for validation styling:

.field-validation-error, .validation-summary-errors,
 label.error {
 color: #fff;
 background-color:#ef425e;
 font-size: 12px;
 font-family: "Open Sans";
 ... (additional CSS styles) ...
}

... (more CSS styles) ...

I am looking to remove or make the selected radio button's validation color disappear through CSS. How can I achieve this without affecting other elements on the page?


UPDATE

The form validation is done using jquery-validate, included in the bundle config for handling validation in Entity Framework models. A custom JavaScript function that previously removed the validation error message from the control was found to be ineffective in removing the red strip still displaying.

$('input[type=radio]').on("change", function (e) {
$(this).closest('div.form-group').find(".field-validation-error").html("");
$(this).closest('div.form-group').find(".field-validation-error").remove();

});

How can I successfully remove a class through JavaScript to make the validation error disappear upon selecting a radio button?

Answer №1

Even after clearing the error box, it remains visible due to the added padding: 7px, ensuring it is always displayed.

To eliminate the class and its styling, follow these steps:

$(this).closest('div.form-group').find(".field-validation-error").removeClass("field-validation-error")

However, this approach means content cannot be added back if the user enters an incorrect value since the class is removed. Therefore, consider adding a class like errorHide and using it to control the element's visibility:

Utilize .errorHide to determine when the styling should be applied

.field-validation-error.errorShow.errorHide{
    display:none;
}

You can then toggle it using jQuery to ensure it is only visible when necessary:

$(this).closest('div.form-group').find(".field-validation-error").toggleClass( "errorHide" )

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

How can I troubleshoot the unresponsive remove div function on my website?

My code is running fine on CodePen (link provided below), but for some reason, it's not working properly in the web browser. I am executing the code from localhost and the button isn't responding as expected. CODE Here is my Visual Studio code ...

What is the best approach for creating a single jQuery click function to manage multiple elements within a slider?

Here's my query: I have a slider on my website where each slider item consists of an image and a Bandcamp iframe link. Below is an example code of one of the slider items: <div class="tapecol col-md-4"> <img class="media-ob ...

Customizing the appearance of the datatable search bar

I've been attempting to customize the appearance of the "Search" text and input field, but they are not responding to my CSS changes. Upon inspecting the elements, I noticed that the datatable filter and info sections have similar styles. Although my ...

The element remains stationary and does not shift positions

My form is not centralizing properly, it seems to be stuck to the left side of #wrapper. I've tried adjusting margin-top but it doesn't seem to affect its horizontal position at all. I've played around with different values for margin and ev ...

"Enhance your webpage with a captivating opaque background image using Bootstrap

I'm new to exploring Bootstrap and I am currently experimenting with options for displaying content with a semi-transparent background image. Currently, I am using a "well" but I am open to other suggestions. I have managed to place the image inside t ...

Steps for aligning an image to the right using Bootstrap Vue

I'm currently learning Bootstrap Vue and I'm trying to align an image on the right using a b-card header. The challenge I'm facing is that when I add the image, it disrupts the perfect size of the template header, causing the image to show a ...

Conceal sub-menu icons within the jQuery User Interface Menu Widget

Is there a way to hide the submenu icon for the jQuery UI menu widget? The default value is set to ui-icon-carat-1-e. $(document).ready(function(){ $(function() { $( "#menu" ).menu({ icons: { submenu: false ...

What is the reason behind Drupal 8 appending a GET variable to the end of each CSS file and resulting in a 404 error?

I've been struggling for hours trying to figure out why the CSS is not loading on the Drupal 8 site I'm migrating. Despite successfully resolving other issues, the CSS files are throwing a 404 error, specifically due to a GET variable (?oe62rp) a ...

What is the best way to show an image within a div using HTML and fetching data from an API response?

I am attempting to showcase an image within a div using HTML. I utilized fetch to retrieve the JSON data from the following API: . The response contains JSON data with the image URL labeled "primaryImage". While I can display it as text, I am encounterin ...

Troubleshooting a Hover Problem in Firefox

Chrome is rendering the following code perfectly fine, but Firefox seems to be having trouble applying the hover effect. HTML <!DOCTYPE html> <html> <head> <link type="text/css" rel="stylesheet" href="stylesheet.css"/> ...

Can we specify the inherit value for font family on an element to ensure consistent style when the specified font is not available?

Suppose I have a page with the following HTML content: <body style="font-family:Helvetica"> <span style="font-family:Segue">Hello World</span> </body> Would it be considered valid to set the font family of the span to Segue, ...

having difficulty applying border color to input when clicked

Is there a way to change the border color of an input field on an onClick event? In my scenario, if the 'Other' radio button is selected and a user attempts to save without entering any value in the input field, I want the border color of the in ...

When you hover over the image, the text will disappear

When I hover over the image of LTC, a thumbnail appears with text and a button. However, I do not want the text "LTC" to be shown when hovering over the image. <div class="col-md-4"> <div class="view view-eighth"> <div class=" ...

Children can easily access multiple items within a list by using the ul tag

I am struggling with changing the class of ul inside the ul.navigator. I want to change it only when I click on a particular li, but my current approach doesn't seem to be working. Can anyone provide some guidance or assistance? $('.navigator& ...

The functionality of the jQuery .click method appears to be malfunctioning within the Bootstrap

It seems like my JQuery.click event is not functioning as expected when paired with the corresponding button. Any ideas on what might be causing this issue? Here's the HTML CODE snippet: <button id="jan7" type="button" class="btn btn-dark btn-sm"& ...

Is it possible for me to customize the CSS of the masterpage for a specific aspx file?

Recently, I completed a website that was almost ready to be published. However, my client now wants to add a new page with similar content as the main masterpage (including the logo, menu, and a few buttons), but with some changes in colors and background ...

What is the best way to adjust the bootstrap column size using variables and decide when to insert a new row dynamically?

I am struggling to dynamically change the number of columns in a row based on the already sorted array. I want the layout to look like the example at the bottom, with Comedies spanning two rows, Action spanning three, and Horror only one row. However, I ca ...

Extremely efficient CSS utility classes for supreme performance

I've noticed the rise of CSS utility classes and I'm curious about their impact on performance. Is there a better approach? Option One - creating HTML elements with multiple utility classes like: <div class="padding flex justifyCenter align ...

Kendo sortable interference with input focus

Utilizing both kendo listview and grid components to display the same information in different views; a primary listview of cards and a table-based grid view, both integrated with the Kendo Sortable widget. The objective is to enable users to edit an inlin ...

Unable to modify the border radius of the carousel indicators in Bootstrap 5

Currently, I am working with the carousel component in Bootstrap 5.1. I have encountered an issue where I am unable to add border-radius to the carousel indicators, despite trying various methods. Even though the inspector shows that the indicators (HTML ...