Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This example shows how to limit the number of characters to be typed into the TextBox using JavaScript and display the remaining characters in a Label Control.

<script type="text/javascript" language="javascript">
   
     function validatelimit(obj, maxchar)
     {
   
        if(this.id) obj = this;
       
        var remaningChar = maxchar - obj.value.length;
        document.getElementById('<%= Label1.ClientID %>').innerHTML = remaningChar;

            if( remaningChar <= 0)
            {
               obj.value = obj.value.substring(maxchar,0);
               alert('Character Limit exceeds!');
               return false;

            }
            else
            {return true;}
     }

    </script>

<body  runat="server">
    <form id="form1" runat="server">

        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onkeyup="validatelimit(this,300)">   </asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="0"></asp:Label>

   </form> 

</body>

Note: 300 is the MAX length of the characters in the TextBox..Just change the value of 300 based on your requirements..

Technorati Tags: ,,
Posted on Tuesday, February 17, 2009 3:44 PM ASP.NET , JavaScript , Tips&Tricks | Back to top


Comments on this post: Restrict number of characters to be entered in the MultiLine TextBox

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
Thank You very much....It is working fine..........
Left by Krishnanunni on May 22, 2009 4:38 AM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
It's Very Nice Article
Left by Madhusudhan on Jun 27, 2009 2:30 AM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
Unfortunately it doesn't seem to work if the users copy and paste into the textbox.
Left by anon on Jul 19, 2009 9:30 PM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
I agree with Anon...
This does not work if the user uses the mouse/keyboard to paste the text in the text box..
Tried a lot for it but could not make it work...
What is the event that will track the pasting in the text box?
can anybody help with it?????
Left by Louie on Jul 30, 2009 2:55 AM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
if i forcibly try to type in more characters as suppose if my maxchar limit is 20 then it will allow me to type in the character though it shows me the alert but still i could enter characters in it..could u please suggest how should i just stop the user from entering characters in it when it exceed the maxchar limit
Left by priyanka on Feb 17, 2010 5:59 PM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
Hi.. i got it... we need to set the MAXLENGTH property for the textbox control to stop entering characters when it exceed the maxchar limit
Left by Priyanka on Feb 17, 2010 8:09 PM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
Very very useful sharing. Thank you very much. It has been very useful for me :))))
Left by cevdet on Aug 17, 2010 6:49 AM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
very good scripts
Left by mahesh on Dec 13, 2010 5:08 PM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
very good thnx alot
Left by ranbeer on Jul 19, 2011 12:23 AM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
what do i put in the 'form id', please?
Left by Nabil on Jan 12, 2012 6:21 PM

# re: Restrict number of characters to be entered in the MultiLine TextBox
Requesting Gravatar...
Its really awesome. Best among all solutons available in google. :)
Left by Prabhu on Jul 28, 2012 7:05 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net