Custom validation rules

4 posts, 0 answers
  1. Sean
    Sean avatar
    7 posts
    Member since:
    Jan 2012

    Posted 01 Feb 2012 Link to this post

    Kendo UI v2011.3.1129

    Hello, I'm trying to attach some custom validation for a text input and having some difficulty.  The rule is that the text field must contain at least one non-whitespace character.  I've tried a dozen different permutations of the snippet below, but it doesn't seem to work quite as expected.

                roleEditObject.formValidator = $("#roleEditorForm").kendoValidator(
                {
                    rules:
                    {
                        custom: function(input)
                        {
                            return input.is("[name=name]") && input.val().match(/^\S+$/);
                        }
                    }
                }).kendoValidator().data("kendoValidator");

    I'm assuming input.is("[name=name]") selects an input with a name attribute equal to 'name'

    Any help would be appreciated, thanks.
  2. Sean
    Sean avatar
    7 posts
    Member since:
    Jan 2012

    Posted 01 Feb 2012 Link to this post

    OK, I'm an idiot.  I figured out what was going on, but for the benefit of anyone else that may have had some confusion here:

                roleEditObject.formValidator = $("#roleEditorForm").kendoValidator(
                {
                    rules:
                    {
                        custom: function(input)
                        {
                            var ret = true;
                            
                            if( input.is("[id=roleNameField]") )
                            {
                                ret = input.val().match(/^\S+$/) != null;
                            }
                            
                            return ret;
                        }
                    },
                    messages:
                    {
                        custom: "Role name must not be blank or contain spaces."
                    }
                }).data("kendoValidator");
  3. Kendo UI is VS 2017 Ready
  4. Chnlove
    Chnlove avatar
    1 posts
    Member since:
    May 2012

    Posted 09 May 2012 Link to this post

    Haha Sean,

    I had confusion on this before and figured out myself as well.

    ---------------------------------------------------------------------------------------------------------------------------------
    Confident man have a happy family with my chnlove girl, trying my best to drive the Chnlove scam away.
  5. ken
    ken avatar
    3 posts
    Member since:
    Aug 2013

    Posted 16 Mar Link to this post

    Custom validators aren't reusable because they must filter by a jquery selector. It is also inefficient to run the validator on every input field. Seems like it would be better to extend Observable:

    <input data-bind="validator: myValidator">

    Another option is to pass the selector to a validation factory as in:

    $("...").kendoValidator({
            rules: {
              x: xValidator("[name=...]")
            }
       });

    function xValidator(selector) {

        return function(input) {

            return input.is(selector) && ...;

        }

    }

Back to Top
Kendo UI is VS 2017 Ready