Pages

Thursday, September 17, 2009

Getting no of characters remaing for a text box..

Many times we set the maximum length of the text box so that a user can only enter that no of specified characters. if you want to so that how many no of character is remaing for that text box , then we can display this using this code..
Here with the use of Javascript we can achieve this task. please check this below code.

Code for ASP page

<head>
<script LANGUAGE="JavaScript">
function textCounter(field,cntfield,maxlimit)
{
if (field.value.length > maxlimit)
{
field.
value = field.value.substring(0, maxlimit);
}
else
{
cntfield.
value = maxlimit -
field.
value.length;
}
}
>
>

<body>
<asp:TextBox ID="txtFeatures" runat="server" TextMode="MultiLine"
Columns
="28" Rows="5" Wrap="true" />
<br />
<input readonly type="text" name="remLen1" id="remLen1" size="3"
maxlength
="3" value="90" />
>


and this code we need to attach with the text box in the cs file.

public void Page_Load()
{
if (!(Page.IsPostBack)) {
txtFeatures.
Attributes("name") = "txtFeatures";
txtFeatures.
Attributes("onKeyUp") = "textCounter(txtFeatures,remLen1,90);";
txtFeatures.
Attributes("onKeyDown") = "textCounter(txtFeatures,remLen1,90);";
txtFeatures.
Attributes("onKeyPress") = "textCounter(txtFeatures,remLen1,90);";
}
}



This is the complete code for the same.. try it..

Happy Coding!!!

Thanks
Anil Kumar Pandey
System Architect
Mumbai, Maharshtra

No comments:

Post a Comment

Kontera