This is a migrated thread and some comments may be shown as answers.

Custom validation rules

3 Answers 620 Views
Validation
This is a migrated thread and some comments may be shown as answers.
Sean
Top achievements
Rank 1
Sean asked on 01 Feb 2012, 04:52 PM
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.

3 Answers, 1 is accepted

Sort by
0
Sean
Top achievements
Rank 1
answered on 01 Feb 2012, 05:34 PM
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");
0
Chnlove
Top achievements
Rank 1
answered on 09 May 2012, 08:25 AM
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.
0
ken
Top achievements
Rank 1
answered on 17 Mar 2016, 02:20 AM

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) && ...;

    }

}

Tags
Validation
Asked by
Sean
Top achievements
Rank 1
Answers by
Sean
Top achievements
Rank 1
Chnlove
Top achievements
Rank 1
ken
Top achievements
Rank 1
Share this question
or