0

I have a kendo grid that needs to display discounts.I have to implement the validation that it should accept numbers between 0.00 and 100. I have written code for accepting numbers between 0 and 100, now i need to implement the 2 decimal place validation as well. Please help.

$(gridname).kendoGrid({
        dataSource: {
            data: data.ReportData,
            schema: {
                model: {
                    fields: {
                        ProposedDiscountNST: {format: "{0:n2}",
                            validation: {
                                required: true,
                                proposeddiscountNSTvalidation: function (input) {
                                    if (input.val() != "" && input.is("[name='ProposedDiscountNST']")) {
                                        input.attr("data-proposeddiscountNSTvalidation-msg", "Should be between 0.00 & 100");

                                     //   return input.val() >= 0 && input.val() < 101 && input.val() % 1 == 0;
                                        return input.val() >= 0 && input.val() < 101 ;   // Accepts max 2 decimal digits
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }

enter image description here

I need to display the validation message that this field accepts 2 decimal places only. Please help.

Tannya
  • 161
  • 6
  • 21

3 Answers3

0

How to obtain the number of decimals is described in multiple places, e.g. Simplest way of getting the number of decimals in a number in JavaScript Get this number and check if is okay or not.

One remark: You are checking if input.val() < 101 This includes 100.7 and does not seem match your requirement "between 0.00 and 100".

Carsten Franke
  • 1,649
  • 2
  • 13
  • 30
0

You can get the number of decimals by comparing the number and the fixed number (number.toFixed(x) rounds the given number to x decimals):

$(gridname).kendoGrid({
    dataSource: {
        data: data.ReportData,
        schema: {
            model: {
                fields: {
                    ProposedDiscountNST: {format: "{0:n2}",
                        validation: {
                            required: true,
                            proposeddiscountNSTvalidation: function (input) {
                                if (input.val() != "" && input.is("[name='ProposedDiscountNST']")) {
                                    input.attr(
                                        "data-proposeddiscountNSTvalidation-msg", 
                                        "Value should be between 0.00 & 100 and have a maximum of 2 decimals"
                                    );
                                    return 
                                            input.val() >= 0 && 
                                            input.val() <= 100 &&
                                            input.val() == input.val().toFixed(2)
                                    ; 
                                } else {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
});
Stephan T.
  • 5,843
  • 3
  • 20
  • 42
-1

Actually I tried the above solution by Stephan T. but unfortunately it did not work. so I tried this method and it worked. So posting it so that it will help some one.

$(gridname).kendoGrid({

        dataSource: {
            data: data.ReportData,
            schema: {
                model: {
                    fields: {
                        ProposedDiscountNST: {format: "{0:n2}",
                            validation: {
                                required: true,
                                proposeddiscountNSTvalidation: function (input) {
                                    if (input.val() != "" && input.is("[name='ProposedDiscountNST']")) {
                                        input.attr("data-proposeddiscountNSTvalidation-msg", "Should be between 0.00 & 100");

                                     //   return input.val() >= 0 && input.val() < 101 && input.val() % 1 == 0;
                                        return input.val() >= 0 && input.val() <= 100 && ((parseFloat(input.val()) / (parseFloat(input.val()).toFixed(2))) == 1 );   // Accepts max 2 decimal digits

                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }

enter image description here

Tannya
  • 161
  • 6
  • 21