【问题标题】:How can restrict two decimal places only in Input Number如何仅在输入数字中限制两位小数
【发布时间】:2021-05-30 04:35:17
【问题描述】:

我怎样才能限制只接受两位小数,或者我怎样才能格式化只有两位小数。我尝试使用模型 RegularExpression(@"^\d+.\d{0,2}$" 并尝试了@step 01 和 tabindex 7,仍然无法正常工作,我们可以为十进制提供两个以上的数字。这是代码

@Html.TextBoxFor(m => m.attendanceLogList[i].NormalHrs, new
 {
       @class = "form-control input-sm emphrs",
       @style = columnstyle,
       @Value = Model.attendanceLogList[i].NormalHrs,
       @type = "number",
       @step = ".01",
       onchange = "CalculateTotal(this);"
   })

【问题讨论】:

    标签: html asp.net-mvc


    【解决方案1】:

    假设 onchange 事件 CalculateTotal(this); 是一个 javascript 函数。恕我直言,以同样的方式将以下 javascript 应用到您的 Razor sn-p 中,应该可以工作,仅供参考。

    function fixDecimal(e) {
      return e.value = e.value.toString().match(/^\d+(?:\.\d{0,2})?/);
    }
    <input type="number" step="0.01" onkeyup="fixDecimal(this)" />

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2019-12-08
      • 2020-03-15
      • 2017-04-01
      • 1970-01-01
      • 2020-12-01
      相关资源
      最近更新 更多