Multiple Validation Rules

3 posts, 0 answers
  1. James
    James avatar
    37 posts
    Member since:
    Sep 2014

    Posted 10 Nov 2014 Link to this post

    Where is the best place for documentation?  I am trying to add multiple rules to my validator but I have something that is causing it to default to the first rule.  Can you help with this?

    01.<script>
    02.    var myValidator = $('#keying_form').kendoValidator({
    03.        rules: {
    04.            custom: function (element) {
    05.                var attr = $(element).attr('checkDates');
    06.                if (typeof attr !== typeof undefined && attr !== false) {
    07.                    console.log('validating a date--> ');
    08.                    console.log(element);
    09. 
    10.                    var value = $(element).val();
    11. 
    12.                    if (value == null || value.length === 0) {
    13.                        console.log('date is empty.');
    14.                        return true;
    15.                    }
    16. 
    17.                    var date = kendo.parseDate(value);
    18.                    if (!date) {
    19.                        console.log('date is invalid!');
    20.                        return false;
    21.                    }
    22. 
    23.                    console.log('date is valid.');
    24. 
    25.                }
    26. 
    27.                return true;
    28.            },
    29.            custom2: function (element) {
    30.                var attr = $(element).attr('checkTime');
    31.                if (typeof attr !== typeof undefined && attr !== false) {
    32.                    console.log('validating a time--> ');
    33.                    console.log(element);
    34. 
    35.                    var value = $(element).val();
    36. 
    37.                    if (value == null || value.length === 0) {
    38.                        console.log('time is empty.');
    39.                        return true;
    40.                    }
    41. 
    42.                    var date = kendo.parseDate(value);
    43.                    if (!date) {
    44.                        console.log('time is invalid!');
    45.                        return false;
    46.                    }
    47. 
    48.                    console.log('time is valid.');
    49. 
    50.                }
    51. 
    52.                return true;
    53.            },
    54. 
    55.        },
    56.        messages: {
    57.            custom: function (element) {
    58.                return $(element).val() + ' is not a valid date.';
    59.            },
    60.            custom2: function (element) {
    61.                return $(element).val() + ' is not a valid time.';
    62.            }
    63. 
    64.        },
    65.    });
    66. 
    67.</script>
  2. James
    James avatar
    37 posts
    Member since:
    Sep 2014

    Posted 10 Nov 2014 in reply to James Link to this post

    I am using MVC for this project.  Below are two of the input items I want to validate.

    <div id="pod">
     
        <div class="left half">
            @(Html.Kendo().DatePickerFor(model => model.PODDate)
                  .Value(DateTime.Today)
                  .HtmlAttributes(new
                  {
                      @style = "width:100%;",
                      checkdates = "true"
                  }))
            <span class="k-invalid-msg" data-for="PODDate"></span>
        </div>
        <div class="right half">
            @(Html.Kendo().TimePickerFor(model => model.PODTime)
                  .HtmlAttributes(new
                  {
                      @style = "width:100%;",
                      checktime = "true",
                  }))
            <span class="k-invalid-msg" data-for="PODTime"></span>
        </div>
    </div>
  3. Kendo UI is VS 2017 Ready
  4. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 12 Nov 2014 Link to this post

    Hi James,

    If I understood correctly the issue is that only the first custom rule is evaluated. That is expected - if there are multiple custom rules, the rules will run in order. The validation will stop at the first rule that fails and display the validation error message associated with that rule.

    For more information please check this help topic: http://docs.telerik.com/kendo-ui/framework/validator/overview#custom-validation-rules

    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top