JavaScript How do I get KendoUI Validator to ignore hidden form elements?

I am attempting to use KendoUI Validator with an ASP.NET WebForms project. I have a simple page, that has a number of inputs, and of course ASP.NET adds some hidden form elements as well.

I have the following questions:

  1. Why does the KendoUI Validator not ignore hidden form fields, and how to I get it to?
  2. Why does KendoUI apply the rules to every input field, and how to do get it to ignore some fields. I want a declarative way to do this, not by adding all sorts of exceptions in my validation rule, as per the example in the KendoUI Validator API page.
  3. Shouldn't it be that if no rule is set as an attribute in the input element (eg; required) then no validation is applied?

Behavior I am getting:

  • With no validation specific attributes on the input element at all, the validation rules still get applied when I call .validate()
  • Hidden form elements are validated.

I am using the following kendo:

http://cdn.kendostatic.com/2013.2.716/js/jquery.min.js
http://cdn.kendostatic.com/2013.2.716/js/kendo.all.min.js
http://cdn.kendostatic.com/2013.2.716/styles/kendo.common.min.css
http://cdn.kendostatic.com/2013.2.716/styles/kendo.default.min.css

I have put together a fiddle that demonstrates this: http://jsfiddle.net/codeowl/B5ML4/3/

And here is the code, for those that don't have access to fiddle:

I have the following markup:

<form action="/" id="testForm">
    <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
    <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

    <input type="text" id="testInput" value="">
    <a id="testValidate" target='_blank' href="javascript:;">Validate</a>
</form>

and the following script:

var validatable = $("#testForm").kendoValidator({
    rules: {
        testRule1: function (input) {
            // Only "Tom" will be a valid value for the FirstName input
            return input.is("[name=firstname]") && input.val() === "Tom";
        },
        testRule2: function (input) {
            return $.trim(input.val()) !== "";
        }
    },
    messages: {
        testRule1: "Your name must be Test",
        testRule2: "Your name must be Foo"
    }
}).data("kendoValidator");

$("#testValidate").click(function () {
    if (validatable.validate()) {
        alert('passed');
    }
});

and when I press the validate link it shows validation messages for the hidden fields.

Answer:1

For anyone interested, I did eventually get a response to this question. I had to post it on the KendoUI Premium Forums to get someone to respond.

Here is the response: How do I get KendoUI Validator to ignore hidden form elements?

Indeed, the hidden input elements are passed through the validation rules logic by default due to the fact that there are multiple widgets which has a hidden input as part of there markup. However, as the built-in rules relays on the presence of certain attributes, if there are missing no validation will happen on the hidden inputs. Therefore, your own custom rules should handle this scenario and skip the appropriate elements. For example:

testRule2: function (input) {
    if (!input.is(":hidden")) {
        return $.trim(input.val()) !== "";
    }
    return true;
}
Answer:2

I'm writing this for new comers.

Simply make hidden inputs disabled

$('#hidden_input').prop('disabled', true) // won't check in kendo or standard jquery validation

$('#hidden_input').prop('disabled', false) // will check in kendo or standard jquery validation
Answer:3

So, the question is: How to stretch tileSprite to fullscreen ? planetSprite = game.add.tileSprite(0, 0, game.width + 500, game.height + 900, planetObject.key + 'Planet'); // planetSprite.anchor.setTo(...

So, the question is: How to stretch tileSprite to fullscreen ? planetSprite = game.add.tileSprite(0, 0, game.width + 500, game.height + 900, planetObject.key + 'Planet'); // planetSprite.anchor.setTo(...

I am using codemirror autocomplete demo. It shows some javascript keywords such as application cache,defaultStatus and framenet and many more suggestions. I want it to display my keywords as ...

I am using codemirror autocomplete demo. It shows some javascript keywords such as application cache,defaultStatus and framenet and many more suggestions. I want it to display my keywords as ...

  1. codemirror autocomplete suggestions
  2. codemirror autocomplete

So, I'm building a map of water taking permits in Toronto. Work in progress available here: http://pennybeames.net/maps/PermitsTO.html I'm using L.mapbox.featureLayer to add my markers: var ...

So, I'm building a map of water taking permits in Toronto. Work in progress available here: http://pennybeames.net/maps/PermitsTO.html I'm using L.mapbox.featureLayer to add my markers: var ...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. canada vaccines
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. cancun
  15. canon
  16. canvas prints
  17. cancer sign
  18. candace owens
  19. canada news
  20. candytopia

According to Wikipedia most browsers use RFC6455 or a slightly earlier version with an almost identical handshake. The server I'm using only supports RFC6455 or similar, where it accepts Origin: or ...

According to Wikipedia most browsers use RFC6455 or a slightly earlier version with an almost identical handshake. The server I'm using only supports RFC6455 or similar, where it accepts Origin: or ...