Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit the length of characters in a textbox in MVC?

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?

like image 483
MrM Avatar asked Dec 18 '09 14:12

MrM


People also ask

How do I limit the length of a TextBox in C#?

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.

How can set maximum length of TextBox in MVC?

The MaxLength for TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html. TextBox and Html.

How can set maximum length of TextBox in asp net?

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.


2 Answers

You need to set some html properties... something like:

<%=Html.TextBox("polNum",null, new {maxlength=10}) %>   

good luck

like image 112
Paul Avatar answered Sep 22 '22 12:09

Paul


Do it in plain HTML:

<%= Html.TextBox("polNum", null, new { @maxlength = "25" }) %>

(The null parameter is because you don't want a default value...)

like image 22
Tomas Aschan Avatar answered Sep 26 '22 12:09

Tomas Aschan