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

问题描述

|
<asp:TextBox ID=\"txtBodySMS\" runat=\"server\" Rows=\"10\"                          
           TextMode=\"MultiLine\" Width=\"100%\"></asp:TextBox>
这是我的文本框。如何限制用户可以在其中输入的字符数?     

解决方法

ѭ1不适用于ASP.NET带有
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>
编辑选项一(服务器端)上面的示例多年来指出了一些问题。潜在的问题是多行“ 6”被呈现为文本区域,而“ 1”被删除。我们也可以通过强制重新附加
MaxLength
而不是
onkeypress
,来修复页面后面的C#或VB asp.net代码。
    protected void Page_Load(object sender,EventArgs e)
    {
        txtBodySMS.Attributes.Add(\"maxlength\",\"10\");
    }
编辑选项二(客户端):这是一个简单的jquery解决方案。在您的头脑中包括以下脚本,而不要附加ѭ9或编辑服务器端代码。
    $(document).ready(function () {
        $(\".MultiLineLimit\").on(\'change keydown paste input\',function () {
            this.value = (this.value.length <= 10 ? this.value : this.value.substring(0,10));
        });
    });
现在,只需将
MultiLineLimit
类添加到
TextMode=\"MultiLine\"
<asp:TextBox>
文本框中。
<asp:TextBox ID=\"txtBodySMS\" CssClass=\"MultiLineLimit\" runat=\"server\" Rows=\"10\" TextMode=\"MultiLine\" Width=\"100%\"></asp:TextBox>
请记住,在任何一种情况下都可以删除客户端验证。如果将其存储在数据库中,则也应始终在服务器端进行验证。     ,这为我做到了。我没有保留MultiLine属性。
<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>
    ,最大字符长度验证(最多允许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>
    ,AFAIK maxlength从未与\“ multiline \”模式一起使用。因此,我建议使用一些客户端js / jquery和服务器端来解决此问题。     ,只需在.cs页面加载中键入以下两行
textbox1.Attributes.Remove(\"MaxLength\");
textbox1.Attributes.Add(\"MaxLength\",\"30\");
希望对您有所帮助!     ,将MaxLength属性设置为字符数。     ,您正在寻找MaxLength:在这里     ,您是否尝试过在TextBox上设置MaxLength属性?以供参考     ,为您的文本框添加类型为FliterTextBoxExtender的扩展器 它应该看起来像
<asp:TextBox ID=\"TxtCellular\" runat=\"server\"></asp:TextBox>
<asp:FilteredTextBoxExtender ID=\"TxtCellular_FilteredTextBoxExtender\" 
                    runat=\"server\" Enabled=\"True\" TargetControlID=\"TxtCellular\" FilterType=\"Numbers\">
</asp:FilteredTextBoxExtender>
    ,重要的是要注意,单独使用ѭ1(不添加其他属性,就像其他答案一样)仅适用于.NET Framework 4.5及更高版本中的项目。 我使用的是4.5,并且适用于我的项目
<asp:TextBox ID=\"txtSample\" MaxLength=\"3\" runat=\"server\"/>
TextBox.MaxLength属性(MSDN文档)