ASP.Net Running total of Character Count on input

  • Thread starter Thread starter BJ
  • Start date Start date
B

BJ

Ever see those nifty sites that allow you to type text into a single or
multi line text box and decrement from the maximum number of characters
to 0?

This lets the user know they are approaching the maximum length to the
textbox. Does anyone have any examples of this script? It is for an
ASP.Net page that has some client side java script.

TIA

BJ
 
Try this (in VB script)...
TextBox1 is the textbox you are monitoring, TextBox2 is a textbox you are
displaying the current count in.

<script language="vbscript">
maxLength = 500
Private sub mytextbox_onkeypress ()
document.all.TextBox2.value = maxLength - len(document.all.TextBox1.value)
end sub
</script>

be sure to add the following to your textbox (client side)
onkeyup="mytextbox_onkeypress"
 
Jim,

Thanks for the response. That didn't work but I do not think it is the
code.
Error:
Line 146
Char: 1
Error: 'mytextbox_onkeypress' is undefined
code: 0
URL:http://localhost/chem/Default.aspx


My page is an ASP.Net page (called Defalut.aspx) that hosts a user
control that contains input fields. So on the HTML of the contorl I
coded the following:

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="UnauthorizeChem.ascx.vb" Inherits="Chem.UnauthorizeChem"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<h1 style="FONT-FAMILY: Verdana">UnAuthorize a Chemical Product
Application</h1>

<input id="textbox2" onkeyup="mytextbox_onkeypress" />
<BR>
<script language="vbscript">
maxLength = 500
Private sub mytextbox_onkeypress()
document.all.TextBox1.value = maxLength -
len(document.all.textbox2.value)
end sub
</script>
<BR>
<BR>
<BR>
<BR>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

I tried to place the script in the head section of the Default.aspx
page but that only locked up my page.
 
I'm not sure, but try removing the / at the end of your text box
definition...
<input id="textbox2" onkeyup="mytextbox_onkeypress">

here is what I had...

<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="vbscript">
maxLength = 500
Private sub mytextbox_onkeypress ()
document.all.TextBox2.value = maxLength - len(document.all.TextBox1.value)
end sub
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<input type="text" id="TextBox1" size="80"
onkeyup="mytextbox_onkeypress">
<input type="text" id="TextBox2" >
</form>
</body>
</HTML>
 
Back
Top