<asp:TextBox ID="txtBodySMS" runat="server" Rows="10"
TextMode="MultiLine" Width="100%"></asp:TextBox>
这是我的文本框。我如何限制用户可以在其中键入的字符数?asp文本框限制字符数?
<asp:TextBox ID="txtBodySMS" runat="server" Rows="10"
TextMode="MultiLine" Width="100%"></asp:TextBox>
这是我的文本框。我如何限制用户可以在其中键入的字符数?asp文本框限制字符数?
<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" MaxLength="2"
Width="100%"></asp:TextBox>
的MaxLength = “的Int32”
<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" MaxLength="220"
TextMode="MultiLine" Width="100%"></asp:TextBox>
您正在寻找的MaxLength:看here
AFAIK maxlength从来没有与“多行”模式一起使用。因此,我会建议一些客户端js/jquery和服务器端来解决这个问题。
添加型FliterTextBoxExtender的扩展您的文本框中
它应该出现这样的
<asp:TextBox ID="TxtCellular" runat="server"></asp:TextBox>
<asp:FilteredTextBoxExtender ID="TxtCellular_FilteredTextBoxExtender"
runat="server" Enabled="True" TargetControlID="TxtCellular" FilterType="Numbers">
</asp:FilteredTextBoxExtender>
MaxLength
并不适用于TextMode="MultiLine"
。一个简单的方法来做到这一点并保持你的MultiLine
马克了将补充:
onkeypress="return this.value.length<=10"
10为限。像这样:
<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" onkeypress="return this.value.length<=10" TextMode="MultiLine" Width="100%"></asp:TextBox>
最大字符长度验证(允许的最大8个字符)
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox1" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{0,8}$" runat="server" ErrorMessage="Maximum 8 characters allowed."></asp:RegularExpressionValidator>
最小字符长度验证(最少8个字符需要)
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox2" ID="RegularExpressionValidator2" ValidationExpression = "^[\s\S]{8,}$" runat="server" ErrorMessage="Minimum 8 characters required."></asp:RegularExpressionValidator>
最小和最大字符长度验证(需要最少5个字符和最多8个字符)
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "TextBox3" ID="RegularExpressionValidator3" ValidationExpression = "^[\s\S]{5,8}$" runat="server" ErrorMessage="Minimum 5 and Maximum 8 characters required."></asp:RegularExpressionValidator>
这不适用于0个字符。 – 2017-04-11 09:41:02
单独使用MaxLength
(没有其他属性可以添加,就像其他答案一样)仅适用于.NET Framework 4.5及更高版本中的项目,这一点很重要。
我使用的是4.5,它的工作为我的项目
<asp:TextBox ID="txtSample" MaxLength="3" runat="server"/>
中的.cs页面加载
textbox1.Attributes.Remove("MaxLength");
textbox1.Attributes.Add("MaxLength", "30");
希望这将有助于正下方两条线型!
不起作用,他们仍然能够进入超过220个字符 – Beginner 2011-05-24 14:06:45
那么什么是错的,这是微软证明财产 – 2011-05-24 14:10:25
不是,需要拿出多 – Beginner 2011-05-24 14:12:58