Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:36:36+00:00 2026-06-12T15:36:36+00:00

I have a search form & knockout-based grid for results. When search is performed,

  • 0

I have a search form & knockout-based grid for results. When search is performed, there is some server-side validation taking place on asp.net mvc, and if model state is not valid, it is returning list of model errors via JSON.

I have jQuery validation already set up, and default validations (regex, required etc) are automatically mapped using jquery.unobtrusive plugin.
I found $.validate().showErrors({prop:error}) as a way to dynamically show errors based on json response from server, but I’m thinking that this is not proper way to use it for displaying server validation messages, as field cannot be reset afterwards (input-validation-error class is not removed).

I need a working method for setting & resetting errors on client, if such exists in $.validate.

There is example with my problem on jsFiddle: http://jsfiddle.net/goranobradovic/ughCm/

To reproduce it, click add error, then remove error, input stays red.

This is because showErrors function does not add any rules which are triggered by validation, so field stays ‘valid’ and it is not in elements() list which is used in resetForm to remove input-validation-error class from invalid fields.

Basically, I want simple way to add/remove validation rule with custom message which is never satisfied on client, to avoid form submission when I set error manually and having to remove invalid class after removing error message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:36:37+00:00Added an answer on June 12, 2026 at 3:36 pm

    I have solved this by overriding showErrors function in jQuery validator with my own, which is compatible with unobtrusive-generated validation spans, and cleaning up valid fields which have invalid class. It is not very nice workaround but it works.

    Here is jsfiddle with solution: http://jsfiddle.net/goranobradovic/ughCm/5/

    UPDATE: As link to external site is not proper answer according to site guidelines, I’m adding code sample here. For anyone already familiar with jQuery validation, just look at two lines of code in showErrors function. I assigned it to validator with validator.settings.showErrors = showErrors;.

    HTML:

    <form id="experiment" action="/" method="post">
    
    <fieldset>
      <legend></legend>
            <div class="editor-label">
              <label for="Email">Email</label>
            </div>
            <div class="editor-field">
    <input data-val="true" data-val-email="&amp;#39;Email&amp;#39; not valid email address." data-val-required="&amp;#39;Email&amp;#39; is mandatory." id="Email" name="Email" type="text" value=""><span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>        
            </div>
            <div class="editor-label">
              <label for="FirstName">First name</label>
            </div>
            <div class="editor-field">
              <input class="text-box single-line" id="FirstName" name="FirstName" type="text" value="">
              <span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>
            </div>
    
            <div class="editor-label">
              <label for="LastName">Last name</label>
            </div>
            <div class="editor-field">
              <input class="text-box single-line" id="LastName" name="LastName" type="text" value="">
              <span class="field-validation-valid" data-valmsg-for="LastName" data-valmsg-replace="true"></span>
            </div>   
    </fieldset>
        <p>
            <button type="submit" class="save ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-secondary" value="Save" role="button" aria-disabled="false"><span class="ui-button-text">Save</span><span class="ui-button-icon-secondary ui-icon ui-icon-disk"></span></button>
    
        </p>
    </form>
    <br/>
    
    <button id="add">Add error</button>
    <button id="remove">Remove error</button>
    
    <br/>
    <br/>
    Debug:
    <div id="debug"></div>
    

    JavaScript:

    var validator = {};
    
    function addError(e) {
        validator.showErrors({
            "FirstName": "test error"
        });
    }
    
    function removeError(e) {
        validator.showErrors({
            "FirstName": null
        });
        fixValidFieldStyles($("form"), validator);
    }
    
    $(document).ready(function() {
        var $form = $("#experiment");
        // prevent form submission
        $form.submit(function(e) {
            e.preventDefault();
            return false;
        });
        $("#add").click(addError);
        $("#remove").click(removeError);
        $("#debug").html("<h1>Validator properties:</h1>");
        validator = $form.validate();
        validator.settings.showErrors = showErrors;
        for (var i in validator) {
            var row = $("<span></span>").html(i).append("<br/>");
            $("#debug").append(row);
        }
    });
    
    
    function showErrors(errorMessage, errormap, errorlist) {
        var val = this;
        errormap.forEach(function(error, index) {
            val.settings.highlight.call(val, error.element, val.settings.errorClass, val.settings.validClass);
            $(error.element).siblings("span.field-validation-valid, span.field-validation-error").html($("<span></span>").html(error.message)).addClass("field-validation-error").removeClass("field-validation-valid").show();
        });
    }
    
    function fixValidFieldStyles($form, validator) {
        var errors = {};
        $form.find("input,select").each(function(index) {
            var name = $(this).attr("name");
            errors[name] = validator.errorsFor(name);
        });
        validator.showErrors(errors);
        var invalidFields = $form.find("." + validator.settings.errorClass);
        if (invalidFields.length) {
            invalidFields.each(function(index, field) {
                if ($(field).valid()) {
                    $(field).removeClass(validator.settings.errorClass);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On my homepage I have a simple search form. The user can enter some
So I have a search form, and when the search result page loads, there
I desperately need to have my search form's default input text to change based
I have created my application that has search form and below contains results for
I have a search form. I would like when someone type the term, first
I have a search form where user can search for products within their specified
I have a search form on my site that submits the url in the
I have a search form with min and max fields. It queries a MySQL
(Using MySQL and PHP) I have a search form that will allow my users
I'm a RoR beginner and am using Rails 3.2.3. I have a search form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.