0

我有一个需要显示折扣的剑道网格。我必须实现它应该接受 0.00 到 100 之间的数字的验证。我已经编写了接受 0 到 100 之间的数字的代码,现在我需要实现小数点后 2 位验证为出色地。请帮忙。

$(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;
                                    }
                                }
                            }
                        }

在此处输入图像描述

我需要显示该字段仅接受 2 位小数的验证消息。请帮忙。

4

3 回答 3

0

您可以通过比较数字和固定数字来获得小数位数(number.toFixed(x) 将给定数字四舍五入为 x 小数):

$(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;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
});
于 2018-10-30T08:01:17.853 回答
0

如何获取小数位数在多处描述,例如JavaScript 中获取数字中小数位数的最简单方法获取此数字并检查是否正常。

一句话:您正在检查input.val() < 101这是否包括100.7并且似乎不符合您的要求“介于 0.00 和 100 之间”。

于 2018-10-30T07:30:35.480 回答
-1

实际上,我尝试了 Stephan T. 的上述解决方案,但不幸的是它不起作用。所以我尝试了这种方法,它奏效了。所以张贴它,这样它会帮助一些人。

$(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;
                                    }
                                }
                            }
                        }

在此处输入图像描述

于 2018-10-30T08:36:18.387 回答