locked
multiline textbox maxlength asp.net textbox RRS feed

  • Question

  • User-1474096950 posted

    setting maxlength of asp.net textbox

    maxlength set thr' properties not working  

    Monday, September 27, 2010 2:22 AM

Answers

  • User-1474096950 posted

    hi thanks  emmuasp

    its working , fine & properly   . the error was because this text box was not taken

    <asp:TextBox ID="limit" Text="125" runat="server" />   

    its wroking fien & perfect as needed

    thns  alot

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 28, 2010 1:38 AM

All replies

  • User604186779 posted

    setting maxlength of asp.net textbox

    maxlength set thr' properties not working  

    Check this

    http://michaelsync.net/2007/04/04/tipstricks-how-to-control-maxlength-of-multiline-textbox-in-aspnet-11

    Monday, September 27, 2010 3:33 AM
  • User-1474096950 posted

    <

    asp:TextBox ID="txtsms" runat="server" Height="104px" Width="350px" TextMode="MultiLine"

     

    MaxLength="160" onkeypress="return textboxMultilineMaxNumber(this,10);" onKeyDown="textCounter(this.id,'remLen1',0);"

     

     onKeyUp="textCounter(this.id,'remLen1',0);" onChange="textCounter(this.id,'remLen1',0);"></asp:TextBox>

     

    can type beyond the specided maxlength

     

    Monday, September 27, 2010 5:53 AM
  • User604186779 posted

    onkeypress="return textboxMultilineMaxNumber(this,10);"

    Not beyond the 10 characters have you checked ?

    Monday, September 27, 2010 6:11 AM
  • User-1474096950 posted

    sorry   i have used 160, onkeypress="return textboxMultilineMaxNumber(this,160);"

    but i can type beyind 160

    Monday, September 27, 2010 6:25 AM
  • User-399422018 posted

    For MultiLine TextBox MaxLength wont work

    So try this way,

    .aspx

    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtString" runat="server"></asp:TextBox>
            <asp:Label ID="lblErrorMessage" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>

    .cs

    if (txtString.Text.Length > 160)
            {
                lblErrorMessage.Text = "String maximum length must be below 2000 characters";
            }


    Let me know if any Q?
     

    Monday, September 27, 2010 6:36 AM
  • User-1561814533 posted

    Have a look at this http://forums.asp.net/p/1597649/4055931.aspx


    The maxlength does not work on multiline textboxes by design. You can create javascript functions to check the length client side, but these will fail if the user turns off javascript, and often do not take into account a user pasting data into the field.


    Monday, September 27, 2010 6:36 AM
  • User-126497635 posted

    <asp:RegularExpressionValidator ControlToValidate="TextBoxID" Display="Dynamic" ValidationExpression="^[\s\S]{0,1000}$" runat="server" />


    Monday, September 27, 2010 6:36 AM
  • User-1474096950 posted

    hello

    its working t th emax length i needed

    when i manually type in , & if the maxlength is attained than nor more characters can be typed

    but the prblm is that  when any matter is copied/pasted & if its greater than the specified max length than also the words gets copied in the textbox

    Monday, September 27, 2010 6:47 AM
  • User1584448589 posted

    Hi svibuk,

    Maxlength doesnt work for  Text Area i.e. for textbox with multiline =true


    in aspx page:

    <asp:TextBox ID="txtArea" runat="server" TextMode="MultiLine"></asp:TextBox>

    -----------------------------------------------------------------------------------------------------

     javascript function in <script> tag as:

    function maxCharLength(t) {

                if (t.value.length >= 10)

                    return false;

            }

    ----------------------------------------------------------------------------------------

    in codebehind in page load event:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            txtArea.Attributes.Add("onkeypress", "return maxCharLength(this);")

        End Sub


    And ur done!!!!!!!!!

    Happy Programming




    Monday, September 27, 2010 6:52 AM
  • User-126497635 posted

    use above regular expression. 100% sure it will work.


    Monday, September 27, 2010 7:26 AM
  • User-399422018 posted

    try this its working for when we copy more characters and paste alsp.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
        <!-- Script by hscripts.com -->
    
        <script type="text/javascript">
    var count = "125";
    function limiter(){
    var tex = document.getElementById('comment').value;
    var len = tex.length;
    if(len > count){
            tex = tex.substring(0,count);
            document.getElementById('comment').value =tex;
            return false;
    }
    document.getElementById('limit').value = count-len;
    }
    
        </script>
    
    </head>
    <body>
        <form name="myform" runat="server">
        <asp:TextBox ID="comment" runat="server" onkeyup="limiter()" TextMode="MultiLine" />
        <asp:TextBox ID="limit" Text="125" runat="server" />
        </form>
    </body>
    </html>
    


     

    Monday, September 27, 2010 7:54 AM
  • User-1474096950 posted

     its working fine ,. thanks a lot

    its working but i am getting a error

    Error: document.getElementById("limit") is null
    Source File: http://localhost:3648/sms.aspx
    Line: 164

    Monday, September 27, 2010 8:37 AM
  • User-399422018 posted

    Can you paste your source code.

    for me its working fine. 

    Monday, September 27, 2010 8:44 AM
  • User604186779 posted

    sorry   i have used 160, onkeypress="return textboxMultilineMaxNumber(this,160);"

    but i can type beyind 160

    Try this it wil be helpful

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=396

    Monday, September 27, 2010 11:53 PM
  • User1584448589 posted

    U are using Master Page-Content Page i think, in such case u need to use ClientID for every server controls in document.getElementByID function

    so ur this line:    document.getElementById("limit")

     needs to b changed like:


    document.getElementById('<%= limit.ClientID %>') , for c# it is case sensetive and for Vb.net it is not.

    if ur using framework 4.0, u can make setting in web.config. so that while rendering id of server controls will remain same and will not change.


    Tuesday, September 28, 2010 12:34 AM
  • User-1474096950 posted

    hi thanks  emmuasp

    its working , fine & properly   . the error was because this text box was not taken

    <asp:TextBox ID="limit" Text="125" runat="server" />   

    its wroking fien & perfect as needed

    thns  alot

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 28, 2010 1:38 AM