Reputation: 22009
I would like to limit a textbox to 10 characters in MVC.
<label ID="lbl2" runat="server" Width="20px"></label>
<%=Html.TextBox("polNum") %>
<label ID="lbl1" runat="server" Width="10px"></label>
I know you can set the Max Length property in .net. How do I do that in MVC with a textbox generated this way?
Upvotes: 10
Views: 32647
Reputation: 11
@Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control", @maxlength = "10" } })
Here i use html.Editor (do not use Textboxfor) For and i am use the Telephone number field in my database. i do not want the user to type more than ten numbers for telephone.
Upvotes: 0
Reputation:
Use below html for set max length of TextBox In Html.TextBox second parameter is value for textbox So, you can pass "" or null
<%=Html.TextBox("polNum", "", new { maxlength = 10 }) %>
Upvotes: 1
Reputation:
<%=Html.TextBox("polNum", new { maxlength = 10 }) %>
http://msdn.microsoft.com/en-us/library/dd492984.aspx
HtmlHelper uses reflection to examine the anonymous type. It converts the fields of the type into attributes on the, in this case, TextBox control. The resulting HTML looks like
<Textbox id="polNum" maxlength =10 />
You can use the anonymous type to add other relevant attributes, such as
new { @class = "MyCssClass", type = "password", value="HurrDurr",
textmode="multiline" }
Upvotes: 2
Reputation: 60674
Do it in plain HTML:
<%= Html.TextBox("polNum", null, new { @maxlength = "25" }) %>
(The null
parameter is because you don't want a default value...)
Upvotes: 3
Reputation: 48108
Use the overload of the TextBox method which is getting Html attributes :
Html.TextBox( "polNum", "value", new { maxlength="10" } );
Upvotes: 1
Reputation: 5576
You need to set some html properties... something like:
<%=Html.TextBox("polNum",null, new {maxlength=10}) %>
good luck
Upvotes: 16