我有这两个文本框,我需要它们只允许输入小数点后两位。 我假设 jQuery 或 Javascript 事件可以做到这一点。我只是不确定如何在 Javascript 中调用文本框。 任何帮助将不胜感激
@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表示小数点后的最小和最大位数。所以根据您的要求进行更改。
关于javascript - 设置文本框接受 2 位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18235111/