将的textboxfor设置为接受小数点后2位

set textboxfor to accept 2 decimal places

本文关键字:小数点 2位 textboxfor 设置 将的      更新时间:2023-09-26

我有这两个textboxfor,我需要它们只允许输入小数点后2位。我假设jQuery或Javascript事件可以做到这一点。我只是不知道如何在Javascript中调用textboxfor。如有任何帮助,将不胜感激

@Html.TextBoxFor(m => Model.Categories[c].EstimateGroups[g].EstimateItems[i].PerUnitCost,
                 new { @disabled = "disabled", @onchange = "costUpdate(this);", @cstType = "perUnit",@id="perUnit"})
<span class="sideLabel">@Model.Categories[c].EstimateGroups[g].EstimateItems[i].PerUnitDescription</span>
if (Model.Categories[c].EstimateGroups[g].EstimateItems[i].Units != null
 && !Model.Categories[c].EstimateGroups[g].EstimateItems[i].IsBasedOnHomeSquareFootage)
{ 
    @Html.TextBoxFor(m => Model.Categories[c].EstimateGroups[g].EstimateItems[i].Units,
                     new { @disabled = "disabled", @onchange = "costUpdate(this);", @cstType = "units" })
}

您可以使用RegularExpression属性。这将完成任务。

[RegularExpression(@"^'d+.'d{0,2}$", 
             ErrorMessage = "It cannot have more than one decimal point value")]
public double PerUnitCost {get;set;}   

这里,在{0,2}中,0和2表示小数点后的最小和最大数字。所以根据您的要求进行更改。