MaxLength property does not work for multiline textbox asp.net

Answered MaxLength property does not work for multiline textbox asp.net

  • 28 Maret 2012 18:57
     
     
    MaxLength property does not work for multiline textbox asp.net i have tried all the verions of visual studio event i can control using .java script but direct it does not wok.

Semua Balasan

  • 29 Maret 2012 8:57
     
     Jawab

    Hi Gurprem,

    you can do it using javascript.

    check below link

    http://dev-eloper.blogspot.in/2009/10/maxlength-not-working-in-aspnet-textbox.html

    Regards

    Sneha

  • 30 Maret 2012 8:26
    Moderator
     
     Jawab Memiliki Kode

    Hi Gurprem,

    Welcome to the MSDN forum!

    Based on my research, you can use JavaScript function or RegularExpressionValidator instead.

    Please refer to the following links:

    Multi Line TextBox Max Length not Working???
    http://forums.asp.net/t/1170096.aspx/1  

    Check maxlength of multiline textbox in javascript
    http://www.codeproject.com/Articles/26179/Check-maxlength-of-multiline-textbox-in-javascript 

    Add the following to your Multiline box in aspx page:

    <asp:TextBox Rows="5" Columns="80" ID="txtCommentsForSearch" MaxLength="10" onkeyDown="return checkTextAreaMaxLength(this,event,'10');"  TextMode="multiLine" CssClass="textArea" runat="server"> </asp:TextBox>

    *txtCommentsForSearch-This is asp.net control that is having multiline property set.

    I have used MaxLength='1999', same property you have to use in underlying javascript file also. I have also passed this length to the calling javascript method, so that in case the MaxLength is not accessible then can be picked from parameters of javascript method.

    Add the following to your javascript file:

    // JScript File
    
    function checkTextAreaMaxLength(textBox, e, length) {
    
        var mLen = textBox["MaxLength"];
        if (null == mLen)
            mLen = length;
    
        var maxLength = parseInt(mLen);
        if (!checkSpecialKeys(e)) {
            if (textBox.value.length > maxLength - 1) {
                if (window.event)//IE
                {
                    e.returnValue = false;
                    return false;
                }
                else//Firefox
                    e.preventDefault();
            }
        }
    }
    
    function checkSpecialKeys(e) {
        if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
            return false;
        else
            return true;
    }     

    Also, for issues about ASP.NET, you may try the ASP.NET forums as well:

    http://forums.asp.net/

    Thanks for your understanding and have a nice day.

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

  • 05 April 2012 2:25
    Moderator
     
     

    Hi Gurprem,

    I temporarily mark the reply as answer to close the thread and you can unmark it if it provides no help.

    For further questions about this issue, please ask them in the dedicated ASP.NET forum:

    http://forums.asp.net/

    Thanks for your understanding and have a nice day.

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

  • 13 Juni 2012 12:19
     
     

    <asp:TextBox ID="txtColumn2" runat="server" TextMode="MultiLine" MaxLength="500" onkeyDown="checkTextAreaMaxLength(this,event,'500');" onblur="onBlurTextCounter(this,'500');"></asp:TextBox>

    function checkTextAreaMaxLength(textBox, e, maxLength) {
        if (!checkSpecialKeys(e)) {
            if (textBox.value.length > maxLength - 1) {
                if (window.event)//IE
                    e.returnValue = false;
                else//Firefox
                    e.preventDefault();
            }
        }
        onBlurTextCounter(textBox, maxLength);
    }
    function checkSpecialKeys(e) {
        if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
            return false;
        else
            return true;
    }

    function onBlurTextCounter(textBox, maxLength) {
        if (textBox.value.length > maxLength)
            textBox.value = textBox.value.substr(0, maxLength);
    }


    SChahal

  • 03 Desember 2012 13:36
     
     

    You have to add following code on page load.

    txtNot.Attributes.Add("MaxLength", "200");