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?
Step 1 : Create a textbox using the TextBox() constructor provided by the TextBox class. // Creating textbox TextBox Mytextbox = new TextBox(); Step 2 : After creating TextBox, set the MaxLength property of the TextBox provided by the TextBox class.
The MaxLength for TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html. TextBox and Html.
Use the MaxLength property to limit the number of characters that can be entered in the TextBox control. This property is applicable only when the TextMode property is set to TextBoxMode. SingleLine or TextBoxMode.
You need to set some html properties... something like:
<%=Html.TextBox("polNum",null, new {maxlength=10}) %>
good luck
Do it in plain HTML:
<%= Html.TextBox("polNum", null, new { @maxlength = "25" }) %>
(The null
parameter is because you don't want a default value...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With