Validation with custom rules failing

2 posts, 0 answers
  1. cra
    cra avatar
    16 posts
    Member since:
    Jun 2012

    Posted 23 Mar Link to this post

    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.

  2. Dimiter Topalov
    Admin
    Dimiter Topalov avatar
    347 posts

    Posted 25 Mar Link to this post

    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!
  3. Kendo UI is VS 2017 Ready
Back to Top