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

    Pertanyaan

  • 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.
    28 Maret 2012 18:57

Jawaban

  • 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

    30 Maret 2012 8:26
  • 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

    29 Maret 2012 8:57

Semua Balasan

  • 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

    29 Maret 2012 8:57
  • 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

    30 Maret 2012 8:26
  • 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

    • Disarankan sebagai Jawaban oleh MicSup 12 Juni 2013 21:45
    05 April 2012 2:25
  • <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

    13 Juni 2012 12:19
  • You have to add following code on page load.

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

    03 Desember 2012 13:36
  • Just throwing a code behind solution to this problem, as an alternative to the Javascript ones already supplied.

    http://radderz.me.uk/2013/09/net-bug-when-adding-maxlength-attribute-to-multiline-textboxes/

    If you add the 'maxlength' attribute via the back end code (such as in the PageLoad or PreRender), this is then recognised. I'm sure it's a bug with the front end of MultiLine TextMode types, but this method works for me.

    // Add MaxLength attribute via code
    Textbox1.Attribute.Add("maxlength", "500");


    • Diedit oleh Radderz81 10 Januari 2014 20:11 Code Styling
    • Disarankan sebagai Jawaban oleh Radderz81 10 Januari 2014 20:11
    10 Januari 2014 20:04
  • Hello Yoyo in sky,

    if i will copy content of greater then 500 characters and pasted it into text box then this method not working.

    pls suggest me solution....


    22 Februari 2014 12:06
  • This is working, Thanks.
    17 Maret 2014 15:37