簡體   English   中英

試圖限制ASP.NET MVC中int數據類型的大小限制

[英]Trying to restrict size limit for int datatype in ASP.NET MVC

我正在嘗試將int數據類型文本框的大小范圍固定為僅兩位數。 但是我不能。 任何想法將不勝感激。

CoilEntry.cs是模型類,其中OUT_P_NO是我想限制為最多兩位數的int屬性。

CoilEntry.cshtml顯示其html部分。

Coilentry.cs (模型類)

public class CoilEntry
{
    [Range(0,99, ErrorMessage="Please use values between 0 to 30")]
    public int OUT_P_NO { get; set; }
}

CoilEntry.cshtml

@using (Html.BeginForm("CoilEntry", "Home", FormMethod.Post))
{
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="3">
                    Coil Entry Detail
                </th>
            </tr>
            <tr>
                <td>
                    OUT PART NO
                </td>
                <td>
                   @Html.TextBoxFor(m=>m.OUT_P_NO,new {@type="number",@maxlength=2,@size=2})
                  </td>
</table>
} 

預期結果將是該文本框僅允許使用0到99之間的值。

當前,文本框僅允許數字值,但可以輸入超過兩位數字的值。

您需要更改為@type =“ text”並為keypress添加事件

將此添加到cshtml文件中頁面的腳本部分。 使用@Html.TextBoxFor(m => m.OUT_P_NO #OUT_P_NO是ID生成

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $('#OUT_P_NO').keypress(function(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
    });
</script>

將type =“ number”更改為“ text”

@Html.TextBoxFor(m => m.OUT_P_NO, new { @type = "text", @maxlength = 2, @size = 2 })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM