如何限制文本框中允许的字符数?

问题描述 投票:21回答:11
<asp:TextBox ID="txtBodySMS" runat="server" Rows="10"                          
           TextMode="MultiLine" Width="100%"></asp:TextBox>

这是我的文本框。如何限制用户在其中键入的字符数?

asp.net
11个回答
20
投票

MaxLength不适用于使用TextMode="MultiLine"的ASP.NET到Textboxes。一个简单的方法来保持你的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>

编辑选项一(服务器端)上面的例子有一些问题多年来指出。根本问题是多线textBox被渲染为文本区域并且MaxLength被移除。我们还可以通过强制重新连接MaxLength而不是附加onkeypress来修复页面后面的C#或VB asp.net代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        txtBodySMS.Attributes.Add("maxlength", "10");
    }

编辑选项二(客户端):这是一个简单的jquery解决方案。在头脑中包含以下脚本,而不是附加onkeypress或编辑服务器端代码。

    $(document).ready(function () {
        $(".MultiLineLimit").on('change keydown paste input', function () {
            this.value = (this.value.length <= 10 ? this.value : this.value.substring(0, 10));
        });
    });

现在只需将MultiLineLimitclass添加到<asp:TextBox>的任何TextMode="MultiLine"文本框中。

<asp:TextBox ID="txtBodySMS" CssClass="MultiLineLimit" runat="server" Rows="10" TextMode="MultiLine" Width="100%"></asp:TextBox>

请记住,在任一情况下,都可以删除客户端验证。如果将其存储在数据库中,它也应始终在服务器端进行验证。


0
投票

为您的文本框添加FilteredTextBoxExtender类型的扩展程序 它应该是这样的

<asp:TextBox ID="TxtCellular" runat="server"></asp:TextBox>
<asp:FilteredTextBoxExtender ID="TxtCellular_FilteredTextBoxExtender" 
                    runat="server" Enabled="True" TargetControlID="TxtCellular" FilterType="Numbers">
</asp:FilteredTextBoxExtender>

0
投票

重要的是要注意单独使用MaxLength(没有其他属性可以添加,就像其他答案一样)仅适用于.NET Framework 4.5及更高版本中的项目。

我正在使用4.5,它适用于我的项目

<asp:TextBox ID="txtSample" MaxLength="3" runat="server"/>

TextBox.MaxLength Property (MSDN Documentation)


17
投票

这样做对我来说。我没有保留MultiLine属性。

<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" MaxLength="2" Width="100%"></asp:TextBox>

11
投票

的MaxLength = “的Int32”

<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" MaxLength="220"                         
           TextMode="MultiLine" Width="100%"></asp:TextBox>

9
投票

最大字符长度验证(允许最多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>

3
投票

AFAIK maxlength从未与“多线”模式配合使用。因此,我建议一些客户端js / jquery和服务器端来解决这个问题。


1
投票

只需在.cs页面加载中键入两行以下

textbox1.Attributes.Remove("MaxLength");
textbox1.Attributes.Add("MaxLength", "30");

希望它会有所帮助!


0
投票

将MaxLength属性设置为字符数。


0
投票

您正在寻找MaxLength:看看here


0
投票

您是否尝试在TextBox上设置MaxLength属性?为Reference

© www.soinside.com 2019 - 2024. All rights reserved.