Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Set maxlength of textarea with javascript

Posted by Viral Sarvaiya on August 11, 2009

Here i demonstrate, how to set maximum length of the textarea or multiline textbox with javascript.

copy this code in the head section of the page.

<script language="javascript" type="text/javascript">
function CheckFieldLength1(fn, mc) {
var len = fn.value.length;
if (len > mc) {
fn.value = fn.value.substring(0, mc);
len = mc;
document.getElementById("charcount").innerHTML = len;
document.getElementById("remaining").innerHTML = mc - len;


now in the body section,

<asp:TextBox ID="txtDesc" runat="server" Width="500" Height="200" TextMode="MultiLine" MaxLength="250"></asp:TextBox><br />
<span><span id="charcount" runat="server">0</span> Characters entered.   |   <span id="remaining"  runat="server">250</span> </span>

in the server side coding, set the 3 attributes as below.

txtDesc.Attributes.Add("onkeyup", "javascript:return CheckFieldLength(" & txtDesc.ClientID & ",250);")
txtDesc.Attributes.Add("onkeydown", "javascript:return CheckFieldLength(" & txtDesc.ClientID & ",250);")
txtDesc.Attributes.Add("onmouseout", "javascript:return CheckFieldLength(" & txtDesc.ClientID & ",250);")

this will allow you to enter only 250 character in the textarea.

enjoy coding…….


3 Responses to “Set maxlength of textarea with javascript”

  1. annesyKet said

    nice design!= and greate news

  2. said

    realy good information

  3. ansari said


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: