textarea counter with ASP.net forms

liunx

Guest
I am using ASP.NET in Visual Studio .NET, I am making a form that contains a text box. For that I used a multiline textbox. My problem is that I want it to have a maximum of 125 characters.

I found some java script code on the internet but it is for html could some one help me how i will change this to asp:textBox

here is the javascript

function textCounter(field, countfield, maxlimit)
{
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}

I used this code for textbox but it is giving an error



<asp:textbox id="txtDes" name ="message1" onKeyDown="textCounter(document.myForm.message1,document.myForm.remLen1,125)"
onKeyUp="textCounter(document.myForm.message1,document.myForm.remLen1,125)" runat="server" Width="488px" Height="138px" TextMode="MultiLine"></asp:textbox></TD>


<td><input readonly type="text" name="remLen1" size="4" maxlength="3" value="125" style="FONT-SIZE:x-small;FONT-FAMILY:arial">characters left</td>

instead of this

<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>





this is giving an error. Please help meyou can use a Page.RegisterClientScript() method which will allow you to add custom attributes to the textbox or other control to validate it.this link will take you to a custom control which will be exactly what you are looking for:

<!-- m --><a class="postlink" href="http://www.codeproject.com/aspnet/textlengthvalidator.asp">http://www.codeproject.com/aspnet/textl ... idator.asp</a><!-- m -->

just download the small amount of files and your good to go! :)
 
Back
Top