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:
<input name=”nbcar” size=”3″ type=”text” /></td>
</tr>
<tr>
<td>
<!– set the name”nbcar_rest” in html control –>
Remaining characters:
<input name=”nbcar_rest” size=”3″ type=”text” /> </td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
OUPUT
After enter some comments:
with regards
vik





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.
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
Pingback: i like reading
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??