1
Please add these attributes in textbox,
- onkeypress
- onkeyup
- onkeydown
- onPaste
asp:textbox id="txtValue" runat="server" onkeypress="return CheckLength(this,150)" onkeyup="return CheckLength(this,150)" onkeydown="return CheckLength(this,150)" onPaste="return CheckLength(this,150)"></asp:textbox>
[ Javascript code ]
function CheckLength(txt, maxLen) {
try {
if (txt != null) {
var iLength = txt.value.length
if (iLength <= maxLen) //Check the Limit.
{
//Display the remaining characters
document.getElementById('<displayControlid>').innerHTML = maxLen - iLength + " are remaining characters.";
}
else {
txt.value = txt.value.substring(0, maxLen);
return false;
}
}
}
catch (e) {
return false;
}
}
1
hi amit, how r u. long long time u give reply to my post.
if we not using java script we not give a maxium characters limit? and i am using maxlenght property of textbox. but after characters reach max limit then also we enter values into textbox it takes the characters. why?
Thanks
1
Hi,
Below one for me. As I understood your need, you don't need to display the total text count, but you need to display remaining possible text count in side TextBox.
My aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<p>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server" MaxLength="150" Height="207px"
TextMode="MultiLine"></asp:TextBox>
<label id="myLabel" style="color:Red;font-weight:bolder">150</label>
</p>
My Codebehind
if (!IsPostBack)
{
TextBox1.Attributes.Add("onpropertychange", "handleChange(this);");
}
My Javascript
function handleChange(ctrl)
{
var length = ctrl.value.length;
var displayValue = new Number(150) - new Number(length)
$get("myLabel").innerText = displayValue.toString();
}
1
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function Count() {
var v1 = document.getElementById('TextBox1');
var v2 = document.getElementById('lblCount');
v2.innerHTML = v1.value.length;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" OnKeyPress="Count()"></asp:TextBox>
<asp:Label ID="lblCount" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
1
hi friend,
you can limit your text boxes to the 150 characters using javascript..
<SCRIPT LANGUAGE="JavaScript">
<!-- Web Site: The JavaScript Source -->
<!-- Use one function for multiple text areas on a page -->
<!-- Limit the number of characters per textarea -->
<!-- Begin
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}
// End -->
</script>
<form name="myForm"
action="/articles/articles/javascript/dynamictextareacounter.asp?ID=<%=siteID%>"
method="post">
<b>One Function to Count and Limit Multiple Form Text Areas</b><br>
<textarea name="message1" wrap="physical" cols="28" rows="5"
onKeyDown="textCounter(document.myForm.message1,document.myForm.remLen1,125)"
onKeyUp="textCounter(document.myForm.message1,document.myForm.remLen1,125)"></textarea>
<br>
<input readonly type="text" name="remLen1" size="3" maxlength="3" value="125">
characters left
<br>
<textarea name="message2" wrap="physical" cols="28" rows="5"
onKeyDown="textCounter(document.myForm.message2,document.myForm.remLen2,125)"
onKeyUp="textCounter(document.myForm.message2,document.myForm.remLen2,125)"></textarea>
<br>
<input readonly type="text" name="remLen2" size="3" maxlength="3" value="125">
characters left
<br>
<input type="Submit" name="Submit" value="Submit">
<br>
</form>
See DemoPlease mark as answer if it helps.
