5

I'm using MVC 3 with jQuery 1.7.2 and I've implemented some custom validation on client and server side to validate that a certain textarea has text if a checkbox is checked.

My client side code to implement unobstrusive validation looks like this:

 $.validator.addMethod("requiredif", function (value, element, params) {
    if (value) {
      var id = '#' + params["otherproperty"];
      var control = $(id);
      if (control.val() == '') {
        return false;
      }
    }

    return $.validator.methods.required.call(this, value, element, params);
});

$.validator.unobtrusive.adapters.add("requiredif", ["otherproperty"], function(options) {
   options.rules["requiredif"] = options.params;
   options.messages["requiredif"] = options.message;
});

My problem is that even though the validation fires if I subsequently go back and untick the checkbox (i.e. text is no longer required), the validation message stays put and validation is not cleared.

I have tried the following to clear the validation:

 $('#Dashboard').on('change', '#Damaged', null, function () {
   var validator = $('#Gridform').validate();
   validator.resetForm();
 });

But this doesn't make any difference.

What I would like is for the validation to work the same as the standard validation does i.e. when I untick the checkbox or enter content into the textarea the validation is cleared/reset.

5
  • Are you using the "ValidationMessageFor" helper? Commented Jul 27, 2012 at 12:00
  • 2
    The problem is that this requiredif adapter is associated with the textarea, not with the checkbox. Commented Jul 27, 2012 at 12:00
  • 1
    see this anthonyvscode.com/2011/07/14/… Commented Jul 27, 2012 at 12:03
  • Hi Darin, you are correct this was associated with the wrong control !! Two other problems remain though (1) The validation summary remains and (2) the validation does not work in IE7 Commented Jul 27, 2012 at 13:56
  • Thanks Yasser, that code had some good pointers in it Commented Jul 27, 2012 at 13:57

1 Answer 1

6

Try this solution, it is working in my project:

<button type="reset" onclick="$('.text-danger').hide();">Reset</ button>

<button type="submit" onclick="$('.text-danger').show();">Submit</button>

also you can change the selector as in your project, in my case im adding (text-danger) class to my validators as:

@Html.ValidationMessageFor(model => model.fieldName, "", new { @class = "text-danger" })

Also there is a better solution i found by John Culviner and it working perfectly

1- after including the jquery (page end) write this script:

<script>
    //clear validation on reset button clicked
    (function ($) { 
        //re-set all client validation given a jQuery selected form or child
        $.fn.resetValidation = function () {
            var $form = this.closest('form');

            //reset jQuery Validate's internals
            $form.validate().resetForm();

            //reset unobtrusive validation summary, if it exists
            $form.find("[data-valmsg-summary=true]")
                .removeClass("validation-summary-errors")
                .addClass("validation-summary-valid")
                .find("ul").empty();

            //reset unobtrusive field level, if it exists
            $form.find("[data-valmsg-replace]")
                .removeClass("field-validation-error")
                .addClass("field-validation-valid")
                .empty();

            return $form;
        };
    })(jQuery);
</script> 

2- Add on click event to call the Jquery function

<button type="reset" onclick="$(this).resetValidation()" >Reset</button>

See here for article by John

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.