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

Validation with custom rules failing

1 Answer 360 Views
Validation
This is a migrated thread and some comments may be shown as answers.
cra
Top achievements
Rank 1
cra asked on 23 Mar 2016, 07:10 PM

Hello,

I have a validator set up on my form. I have several fields listed as required with data-required-message attributes to display specific messages.

I also have a custom rule to compare two password fields to make sure they are the same:

 

$frmCreateUser.kendoValidator({
        validateOnBlur: true,
        rules: {
            passwordmismatch: function (input) {
  
                if (input.is('[data-passwordmismatch-msg]')) {
                    var password = $.trim($password.val());
                    var password2 = $.trim($password2.val());
  
                    return (password === password2);
                }
            }
        }
    });
    var validator = $formContainer.data('kendoValidator');

 

$frmCreateUser.submit(function (event) {
        event.preventDefault();
  
        console.log('submit');
  
        if (validator.validate()) {
            console.log('valid');
            userViewModel.save();
        } else {
            console.log('invalid!');
        }
    });

 

What is happening is even when all of the required rules pass and the password mismatch rule returns true, the validator still returns false. If I remove the custom rule everything works fine.

Any help would be greatly appreciated.

1 Answer, 1 is accepted

Sort by
0
Dimiter Topalov
Telerik team
answered on 25 Mar 2016, 01:14 PM
Hi Christopher,

The custom rule function should return true in the cases when the if condition false, otherwise the function will return undefined, which is in turn evaluated to false:

rules: {
passwordmismatch: function (input) {
   
if (input.is('[data-passwordmismatch-msg]')) {
                                   ...
  }
       
       
return true;
}
...

The following links contain the corresponding section of our documentation and a demo:

http://docs.telerik.com/kendo-ui/controls/editors/validator/overview#custom-rules-for-validation

http://demos.telerik.com/kendo-ui/validator/custom-validation

I hope this helps.

Regards,
Dimiter Topalov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Validation
Asked by
cra
Top achievements
Rank 1
Answers by
Dimiter Topalov
Telerik team
Share this question
or