Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation validates disabled input fields

I have set up validation for my form using jQuery validation. The unfortunate thing is that at some points I want to call $("#clientConfigurationForm").valid() to force re-evaluation when a user enables / disables a check box. I want input forms that are currently invalid to be ignored (and lose the validation state) when they are disabled and .valid() is called. Currently this isnt happening. How can I get this to work?

Checkbox events

$("#clientConfigurationForm").on("change","input[type=checkbox]",function(){
    $(this)
        .closest("div")
        .find("input, select")
        .not(this)
        .not(":submit")
        .prop("disabled", !$(this).prop("checked"))
        .each(function(){
            $(this).valid();
        });
});

Validation Code

$("#clientConfigurationForm").validate({
    onkeyup: function (element) {
        $(element).valid();
    },
    focusInvalid: false,
    invalidHandler: function(form, validator) {
        if (!validator.numberOfInvalids())
            return;

        // Find out what my ID is
        var formIndex = $(validator.errorList[0].element).prop("id").replace(/[A-Za-z]+/g, '');
        $("#clientSettingsLinks a").eq(formIndex).trigger("click");
    },
    errorPlacement: function (error, element) {
        var parent = $(element).parent();
        var validationContainer = parent.find(".validationResult");

        if (validationContainer.length == 0){
            validationContainer = $('<span class="validationResult"><i class="'+errorIconClass+' inputValidationIcon"></i></span>');
            parent.append(validationContainer);
        }

        validationContainer.addClass("tooltip").attr("title", error.text());
    },
    highlight: function (element, errorClass, validClass) {
        $(element).parent().find("i").removeClass("icon-ok").addClass(errorIconClass);

        $(element).addClass("error");
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).parent().find("i").removeClass(errorIconClass).addClass("icon-ok");
        $(element).parent().find("span.validationResult.tooltip").removeClass("tooltip").attr("title", "");

        $(element).removeClass("error");
    },
    submitHandler: function(form) {
        alert("SUBMITTED YO");
    }
});
like image 512
Chris Avatar asked Oct 03 '22 00:10

Chris


1 Answers

Documentation: Inputs of type submit and reset are always ignored, so are disabled elements.

However it seems you have found something weird.

This works

Live Demo

var val;
$("#clientConfigurationForm").on("change", "input[type=checkbox]", function () {
    val.resetForm();
    $(this)
        .closest("div")
        .find("input, select")
        .not(this)
        .not(":submit")
        .prop("disabled", !$(this).prop("checked"))
        .not(":disabled")  
        .each(function () {
          $(this).valid();
        });
});

val = $("#clientConfigurationForm").validate({

    onkeyup: function (element) {
        $(element).valid();
    },
    focusInvalid: false,
    submitHandler: function (form) {
        alert("submitted");
    }
});
like image 169
mplungjan Avatar answered Oct 07 '22 16:10

mplungjan