MaxLength ignored in multiline textbox.

Last post 10-22-2009 6:46 AM by Shrikant_kalwade. 12 replies.

Sort Posts:

  • MaxLength ignored in multiline textbox.

    04-22-2008, 3:10 PM
    • Contributor
      2,838 point Contributor
    • khaos
    • Member since 09-12-2003, 1:59 AM
    • USA Suffolk, Virginia
    • Posts 658

    Problem : Maxlength property is ignored in multiline textboxes. Angry

    Details of problem : When the TextMode property of a textbox is set to Multiline the MaxLength property will not limit the maximum length of user input.  This is due to the fact that multiline textboxes are rendered to the browser actually as a textarea which has no maxlenth property. (I believe a visual cue, like disable maxlength, when multiline is selected would be an added value.)

    Steps to reproduce this issue (for those who don’t know that its a problem)

    1. Add a Multiline textbox (txtMl.TextMode = Multiline)
    2. Add another singleline textbox (txtSl.TextMode = SingleLine).
    3. Set the MaxLength of Both to 10.
    4. Run the project
      1. Observe: You can type as many characters as you wants in the multiline Textbox but not in the singleline Textbox

    Solution ~

    This problem can be solved by using Javascript to track how many characters typed in textbox. If the length of characters are over max number then we can simply ignore what the user type by returning “false” in javascript function.

    Step #1. Put the following function in your page.

    <script language="javascript">  
        function textboxMultilineMaxNumber(txt,maxLen){  
            try{  
                if(txt.value.length > (maxLen-1))

                return false;  
               }catch(e){  
               }  
        }  
    </script> 


    <script language="javascript">
        function textboxMultilineMaxNumber(txt,maxLen){
            try{
                if(txt.value.length > (maxLen-1))return false;
               }catch(e){
            }
        }
    </script>

    Step #2. Put the JS function in onkeypress event of Textbox.


    <asp:textbox id="txtMl" runat="server" Width="232px" TextMode="MultiLine" 
    onkeypress=<strong>"return textboxMultilineMaxNumber(this,10)"</strong>></asp:textbox> 

    As a side note remember to validate your data server side as javascript can be and often is disabled ... 

    Run the sample

    Observe: Both textboxes restrict the characters you type to 10 as desired.

    The main idea for this tip comes from Mike Sync's site.  I just wanted to make a non download type of tip out of it.

    HTH,

    Joe Johnston
    If a picture is worth a 1000 words, a sample application is worth a 1000 blog entries
    Remember the Constitution!
    Dont forget to mark posts answered and resolved (if they are)
  • Re: MaxLength ignored in multiline textbox.

    04-22-2008, 5:21 PM
    • Contributor
      3,196 point Contributor
    • DigiMortal
    • Member since 01-10-2007, 7:22 PM
    • Tallinn, Estonia
    • Posts 562
    • TrustedFriends-MVPs
    For multiline Textbox you should set Columns and Rows properties. Columns*Rows=max length of multiline Textbox text. Single line textbox is rendered as <input type="text"> and multi line textbox is rendered as TextArea.
    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
  • Re: MaxLength ignored in multiline textbox.

    04-23-2008, 2:22 AM
    • Member
      9 point Member
    • baijunagori
    • Member since 09-12-2005, 2:56 PM
    • Dallas, TX
    • Posts 5

    You can use a regular expression validator instead 

     <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtGoals"
                        Display="Dynamic" ErrorMessage="Please limit to 1500 characters or less."
                        ValidationExpression="[\s\S]{1,1500}"></asp:RegularExpressionValidator>
     

    Only problem is it wont tell you when you reached the limit unless you change focus.

     

    Baiju Nagori

  • Re: MaxLength ignored in multiline textbox.

    05-21-2008, 3:05 AM
    • Member
      25 point Member
    • smoothmovz
    • Member since 04-17-2008, 8:56 AM
    • Philippines
    • Posts 34

    <asp:textbox id="txtMl" runat="server" Width="232px" TextMode="MultiLine" onkeypress="return textboxMultilineMaxNumber(this,10)"></asp:textbox>

  • Re: MaxLength ignored in multiline textbox.

    06-09-2008, 6:20 PM
    • Member
      57 point Member
    • atikatak
    • Member since 05-30-2006, 4:11 AM
    • Posts 18

    Thanks for your infomation! ;)

  • Re: MaxLength ignored in multiline textbox.

    08-19-2008, 9:00 AM
    • Member
      16 point Member
    • Sanotsh
    • Member since 08-19-2008, 12:57 PM
    • Posts 20

     

    But what if user copy paste text in it?

    I tried this but its allowing me to enter characters more than max limit

    Swanty :)
  • Re: MaxLength ignored in multiline textbox.

    08-19-2008, 11:32 AM
    • Contributor
      2,838 point Contributor
    • khaos
    • Member since 09-12-2003, 1:59 AM
    • USA Suffolk, Virginia
    • Posts 658

    You could include another event like blur to truncate with notification etc.

    I meant to mention that issue.  In the tip.

    hth,

    Joe Johnston
    If a picture is worth a 1000 words, a sample application is worth a 1000 blog entries
    Remember the Constitution!
    Dont forget to mark posts answered and resolved (if they are)
  • Re: MaxLength ignored in multiline textbox.

    09-03-2008, 8:11 PM
    • Member
      9 point Member
    • YdoIbother
    • Member since 05-18-2006, 12:36 PM
    • Posts 5

    Thanks for the info.  VS was giving me a "Validation (ASP.Net): Attribute 'onkeypress' is not a valid attribute of element 'TextBox'." warning, so I added the onKeyPress in the Page_Load instead.  Here's the whole VB page:

     

    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.TextBox1.Attributes.Add("onKeyPress", "return textboxMultilineMaxNumber(this,40);")
        End Sub
        
    </script>
        <script type="text/javascript">  
            function textboxMultilineMaxNumber(txt,maxLen){  
                try{  
                    if(txt.value.length > (maxLen-1))
                    return false;  
                   }catch(e){  
                   }  
            }  
        </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>MaxLength Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
            <asp:TextBox ID="TextBox1" runat="server" Text="Note that you can paste past the limit"
                Width="50%" Rows="5" BackColor="#FFFFCC" TextMode="MultiLine">
            </asp:TextBox>
            
            <br /><br />
            <asp:Button ID="Button1" runat="server" Text="Do Something" />
            <br /><br />
            
             <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
                        Display="Dynamic" ErrorMessage="Please limit to 40 characters or less."
                        ValidationExpression="[\s\S]{1,40}"></asp:RegularExpressionValidator>
        </div>
        </form>
    </body>
    </html>
     
  • Smile [:)] Re: MaxLength ignored in multiline textbox.

    09-05-2008, 1:39 AM
    • Member
      16 point Member
    • Sanotsh
    • Member since 08-19-2008, 12:57 PM
    • Posts 20

    Thanks  :)

    Above code solved my problem of copy pasting the text in text box which exceeds max limit of text box.

     

    Swanty :)
  • Re: MaxLength ignored in multiline textbox.

    09-06-2008, 9:30 AM
    • Member
      17 point Member
    • mpdillon
    • Member since 01-29-2008, 3:18 AM
    • Posts 62

    I wanted to do the same thing this morning, limit the length of a multiline text box but I am a beginner and have two questions.

    Do I add

    <script runat="server">

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.TextBox1.Attributes.Add("onKeyPress", "return textboxMultilineMaxNumber(this,40);")
        End Sub
       
    </script>

    directly onto the .aspx file or does this belong in the .aspx.vb file?

     

    Second. I am using Master pages. Where do these scripts go on the non-Master page. There isn't a  regular header section on the .aspx page when using master pages.

    Do they go here? 

     <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

      <script type="text/javascript"
            function textboxMultilineMaxNumber(txt,maxLen){ 
                try{ 
                    if(txt.value.length > (maxLen-1))
                    return false; 
                   }catch(e){ 
                   } 
            } 
        </script>

     <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        <style type="text/css">
            .style7
            {
                width: 209px;
            }
        </style>
    </asp:Content>

     

    Or do they go here? 

    <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
     <script type="text/javascript"
            function textboxMultilineMaxNumber(txt,maxLen){ 
                try{ 
                    if(txt.value.length > (maxLen-1))
                    return false; 
                   }catch(e){ 
                   } 
            } 
        </script>

         <style type="text/css">
            .style7
            {
                width: 209px;
            }
        </style>
    </asp:Content> 

    Thank you,

    pat

  • Re: MaxLength ignored in multiline textbox.

    09-22-2008, 10:57 AM
    • Contributor
      2,838 point Contributor
    • khaos
    • Member since 09-12-2003, 1:59 AM
    • USA Suffolk, Virginia
    • Posts 658

    You need to take those scripts and place them in your code behind for your content page or your usercontrol whichever you are using.  For your scripts I like to control them a bit more so I add the javascript in code behind using the ClientScript objects see more here http://fuchangmiao.blogspot.com/2008/08/clientscriptmanager.html 

    this code you can add it to your .VB code behind.  There are a few scholls of thought on this but the code behind file is a best practice

    <script runat="server">

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Me.TextBox1.Attributes.Add("onKeyPress", "return textboxMultilineMaxNumber(this,40);")
        End Sub
       
    </script>

    HTH,

    Joe Johnston
    If a picture is worth a 1000 words, a sample application is worth a 1000 blog entries
    Remember the Constitution!
    Dont forget to mark posts answered and resolved (if they are)
  • Re: MaxLength ignored in multiline textbox.

    08-03-2009, 8:13 AM
    • Member
      424 point Member
    • khurramatk2
    • Member since 09-14-2007, 8:43 PM
    • Saudi Arabia
    • Posts 76

    Here is the simplest solution without any need to embed javascript.

    http://dotnetfocus.blogspot.com/2009/08/systemwebuiwebcontrolstextbox-maxlength.html 


    Hope it will help...

    Khurram Shahzad
    K2.Net Workflows
    Dot Net Focus
    Remember to Mark as "Answer" if this helped.
  • Re: MaxLength ignored in multiline textbox.

    10-22-2009, 6:46 AM

    hi khaos,

    I used this code in my project and its work fine with IE. but it is not work with mozilla.

    it limits the character but if i want to delete some character by pressing back space or by Delete button it not works.

    please help me on this issue.

    Thanks and regards in advance

    Shrikant.


Page 1 of 1 (13 items)