【问题标题】:kendo grid cell validation for 2 decimal places小数点后 2 位的剑道网格单元验证
【发布时间】:2018-10-30 06:37:37
【问题描述】:

我有一个需要显示折扣的剑道网格。我必须验证它应该接受 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 位小数的验证消息。请帮忙。

【问题讨论】:

    标签: kendo-ui kendo-grid


    【解决方案1】:

    如何获得小数位数在多处描述,例如Simplest way of getting the number of decimals in a number in JavaScript获取这个号码,看看是否正常。

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

    【讨论】:

    • 是的,我将其更改为
    • 如果该回复或任何其他回复回答了您的问题,请将其标记为“已接受”。这样,其他用户可能会更快地找到最有用和最相关的答案。
    【解决方案2】:

    你可以通过比较数字和固定数字来获得小数位数(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;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    });
    

    【讨论】:

    • 感谢您的回复,但它没有在屏幕上显示错误消息。
    【解决方案3】:

    实际上,我尝试了 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;
                                        }
                                    }
                                }
                            }
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 2015-09-26
      相关资源
      最近更新 更多