How to Control the Length of a multiline text box

Rate It (2)

Last post 01-07-2007 11:26 AM by Jigar. 7 replies.

Sort Posts:

  • How to Control the Length of a multiline text box

    01-05-2007, 5:18 AM
    • Loading...
    • sivasravan
    • Joined on 06-27-2006, 4:43 PM
    • Posts 2

    Hii Smile

    I am doing a project dealt with sending SMS ..
    So i have to control the length of the message to be sent ..
    But for textbox of Multiline type did not allowing the MaxLength property ..
    So plz do favour for me regarding this one ...

    Thanks a lot in advance

    Sravan....
  • Re: How to Control the Length of a multiline text box

    01-05-2007, 7:13 AM
    • Loading...
    • RikardoB
    • Joined on 01-03-2007, 5:43 PM
    • Portugal, Lisbon
    • Posts 41

    Hello Sivasravan!

    You have a little problem. Like you said you are using a TEXTBOX with MULTINE type. Using that object is almost the same thing using a TEXTAREA and this object doesn't have MULTINE type too.

    My sugestion is using a JAVASCRIPT FUNCTION to count the length of the TEXTBOX. Try this:

    <SCRIPT language="text/javascript">
    function CountLength(ObjectThatYouWant)

    {
            LenTxt = ObjectThatYouWant.value.length;
            if (LenTxt > 100)

            {
                ObjectThatYouWant.value = ObjectThatYouWant.value.substring(0,100);
            }

    }

    </SCRIPT>

    Then call this function on client side in this button events : onkeydown, onkeyup, onchange, onblur

     

    HAVE NICE DEBUGS!


  • Re: How to Control the Length of a multiline text box

    01-05-2007, 7:16 AM
    • Loading...
    • RikardoB
    • Joined on 01-03-2007, 5:43 PM
    • Portugal, Lisbon
    • Posts 41
    * TEXTAREA doesn´t have MaxLength property...
  • Re: How to Control the Length of a multiline text box

    01-05-2007, 8:29 AM
    • Loading...
    • akjoshi
    • Joined on 05-06-2006, 1:23 PM
    • INDIA
    • Posts 614

    Hi ,

    I use this function for restricting length of textarea, chek if it works for you

    // Javascript for restricting the maxchars entered in textarea. //

    // onkeypress="return maxtext(this, 256, event);"

    function maxtext(field, size, e)

    {

    tempstr = field.value;

    if(tempstr.length<size){return true;}

    else

    {

    var key;

    if(window.event)

    key = e.keyCode;

    //IE

    else

    key = e.which;

    //FF

    // Allow special characters: BACKSPACE, TAB, RETURN, LEFT ARROW,

    // RIGHT ARROW to go through ** Work in IE only

    if ((key == 8) || (key == 9) || (key == 32) || (key == 40) || (key == 41) || (key == 43) || (key == 45))// || (key == 13))// || (key == 32))

    return true;

    }

    return false;

    }

    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit for being helpful (and makes search more relevant too).
  • Re: How to Control the Length of a multiline text box

    01-05-2007, 10:16 AM
    • Loading...
    • pushp_aspnet
    • Joined on 10-19-2006, 1:27 PM
    • Hyderabad,India
    • Posts 447

    Hi, 

    checkout this link: http://www.dynamicdrive.com/dynamicindex16/maxlength.htm

    Hope this helps. 

    Home Is Where the Wind Blows
    http://pushpontech.blogspot.com
  • Re: How to Control the Length of a multiline text box

    01-05-2007, 10:51 AM
    • Loading...
    • haoest
    • Joined on 10-25-2005, 8:20 PM
    • Posts 403

    make sure you understand that checking on client side is only half of the work. You have to make sure the message is within specified length on server side, too.

     

    Debugger is my best friend. (http://haoest.info)
  • Re: How to Control the Length of a multiline text box

    01-07-2007, 7:21 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 9,403
    • TrustedFriends-MVPs

    In the server-side Page_Load event handler:
         TextBox1.Attributes.Add("maxCharacters", "3"); 
         TextBox1.Attributes.Add("onblur", "if ( this.value.length > this.maxCharacters ) {alert('Too many characters!'); this.value = this.value.substr(0, this.maxCharacters);}");

    NC...

  • Re: How to Control the Length of a multiline text box

    01-07-2007, 11:26 AM
    • Loading...
    • Jigar
    • Joined on 06-17-2002, 5:41 AM
    • Ridgewood, NJ
    • Posts 935

    How about using RegularExpressionValidator to validate length of TextBox it will also take care of serverside validation. following sample code shows how to use that.

    Run sample code

     

    1    <%@ Page Language="C#" %>
    2    <html>
    3    <head id="Head1" runat="server">
    4        <title>Multiline Textbox max length</title>
    5    </head>
    6    <body>
    7        <form id="form1" runat="server">
    8        <div>
    9            <p>Only 25 chars Please!</p>
    10           <asp:TextBox runat="server" ID="MLTextBox" 
    11               TextMode="MultiLine" width="400"></asp:TextBox>
    12           <asp:RegularExpressionValidator 
    13                   runat="server" Display="dynamic" 
    14                   ControlToValidate="MLTextBox" 
    15                   ValidationExpression="^([\S\s]{0,25})$" 
    16                   ErrorMessage="p>">
    17           </asp:RegularExpressionValidator>
    18           <p />
    19           <asp:Button ID="Button1" runat="server" text="Submit"/>
    20       </div>
    21       </form>
    22   </body>
    23   </html>
    
     
    Jigar Desai
    -----------------------
    Do not forget to "Mark as Answer" on the post that helped you.
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter