How to use Asp.net
  • Home
  • About us
  • Disclaimer
  • joy of helping
KEEP IN TOUCH

How can we limit the characters in multiline textbox asp.net using JavaScript

Oct20
2011
4 Comments Written by vikram

Hello there, sometimes we need to restrict the character length in Multiline Textbox and we know we can achieve by our logic in code-behind. Why don’t we try JavaScript? So I found something and modify it according to my need.  Following the code which will restrict the limit of characters in TextBox.

 

Create web form [Default2.aspx] and copy paste the source code in it

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default2.aspx.cs” Inherits=”Default2″ %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

 

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Untitled Page</title>
<script  language=”JavaScript” type=”text/javascript”>
//****  for master page the control name will be changed so you can access those name under source code.
//**** it could  be –> ctl00$ContentPlaceHolder1$txt_commnets so u have to change it where u will use the textbox.
//****  and form name could be –> aspnetForm instead of Form1
var max=50;             // 50  = max characters // we can set according to our need.

function count(f) {
var txt=f.txt_commnets.value; // text box name
var nb=txt.length; // will get the length
if (nb>max) {  // will check the given limit of the characters
alert(“No more than “+max+” characters in this field !”);
f.txt_commnets.value=txt.substring(0,max);
nb=max;
}
f.nbcar.value=nb;
document.forms["form1"].nbcar_rest.value = 50-document.forms["form1"].nbcar.value;
}

function timer() { // timer function for set the timing for rem. characters.      count(document.forms["form1"]);
setTimeout(“timer()”,100);
}
</script>
</head>

<body onLoad=”setTimeout(‘timer()’,100)”>
<form id=”form1″ runat=”server”>
<table>
<tr>
<td>
Enter Comments below:</td>
</tr>
<tr>
<td>
<!– following textbox name we will access in javascript–>
<asp:TextBox ID=”txt_commnets” runat=”server” TextMode=”MultiLine” Height=”99px”     Width=”357px”></asp:TextBox>
</td>
</tr>
<tr>
<td>
<!– set the name”nbcar” in html control –>
Number of characters:&nbsp;
<input name=”nbcar” size=”3″ type=”text” /></td>
</tr>
<tr>
<td>
<!– set the name”nbcar_rest” in html control –>
Remaining characters:&nbsp;&nbsp;
<input name=”nbcar_rest” size=”3″ type=”text” />&nbsp;&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>

OUPUT

After enter some comments:

 

 

with regards

vik

Posted in jQuery
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail
« How can create Google Visualization: Geomap in asp.net
» How to check all CheckBox in GridView in asp.net C#

4 comments on “How can we limit the characters in multiline textbox asp.net using JavaScript”

  1. document character count on November 8, 2011 at 11:28 am said:

    Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but other than that, this is great blog. A great read. I will definitely be back.

    Reply ↓
  2. vinod on November 9, 2011 at 4:01 am said:

    initialy i didnt work(as i am new to javascript)
    but finally it work,take a look at the count function call inside the timer() , the function call is gone along with the comments
    any way thanks for the code

    Reply ↓
  3. Pingback: i like reading

  4. dev on August 17, 2012 at 7:01 am said:

    Hi,Great job done.This thing work perfectly when we type characters.But if we copy a large text data from any document and paste in the text box it is allowing the data tin text box.how to restrict this??

    Reply ↓

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Joy of Helping

Any sort of help to the children who are fatherless, poor & are from far-off areas.
Joy of Helping

Facebook Link

Asp dot Net ,Ajax,Xml.

Recent Posts

  • ROW_NUMBER(), NTILE(), partition by, Duplicate Records, CTE sql server 2008
  • How to get second highest Salary sql server 2008
  • Insert values, insert into, insert default value, insert execute and select into sql server 2008
  • How to Bind GridView with SqlDataReader in Asp.net
  • how to attach files to email without storing on disk using Asp.net FileUpload control

Recent Comments

  • Anonymous on ROW_NUMBER(), NTILE(), partition by, Duplicate Records, CTE sql server 2008
  • dhananjay on How to use GridView with Insert, Edit, Update, Delete the Ado.net way C#
  • dev on How can we limit the characters in multiline textbox asp.net using JavaScript
  • navneet on How to display image in Image control after upload on the server asp.net C#
  • AnhVu on How to do Shopping Cart in Asp.net C#

Archives

  • February 2013
  • September 2012
  • April 2012
  • January 2012
  • October 2011
  • August 2011
  • May 2011
  • April 2011
  • March 2011

Categories

  • Asp.net
  • jQuery
  • Sql Server

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

EvoLve theme by Theme4Press  •  Powered by WordPress How to use Asp.net