Client side character count - Textbox

K

Kevin Spencer

In JavaScript, you can use the length property of the value property of the
textbox to get the number of characters. Wire your Event Handler to the
"onchange" property of the textbox, like so:

<script type="text/javascript"><!--
function countChar(str)
{
window.status= str.length + " characters typed";
}
// --></script>
<input type="text" name="MyText" onchange="countChar(this.value)">

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Stefano Mostarda

If I'm not wrong, there is a maxlength (or something like that) property
in the textbox control.

Stefano Mostarda MCP
Rome Italy
 
J

Jerry

In limiting textbox input to 500 characters I would like to include a
dynamic count of characters input while the user is typing into a textbox.
This would obviously be a client side control, possibly a custom validator
with a function written in javascript.
Has anyone done this? Does someone have an example?

Regards
 
J

Jon

this would only show the character count when the user clicks outside the
textbox - what you probably want is to keep a running tally while the users
typing and maybe display it another form field
<script type="text/javascript">
function countChar(str,f){
f.Count.value = str.length
}
</script>
<form>
You've typed <input size=3 type="text" name="Count" value="0"> characters
<input type="text" name="MyText" onkeyup="countChar(this.value,this.form)">
</form>

Jon
 
K

Kevin Spencer

this would only show the character count when the user clicks outside the
textbox - what you probably want is to keep a running tally while the
users

You have mixed up the lostfocus event with the onchange event. My technique
works just fine, thank you.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Jon

I didn't say yours didn't work :) I said it doesn't keep a running count
while you're typing (which I think is what the poster wants and is probably
easier for the user) Compare mine with yours to see the difference.

What's a lostfocus event - do you mean onblur? Onchange fires when you click
outside a textbox and the text has changed, onblur fires when you click
outside a textbox wether or not the text has changed, onkeyup fires as soon
as you press and release a key.

Jon
 
K

Kevin Spencer

My method will indeed keep a running count while you're typing, as each
character typed changes the value of the textbox.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
K

Kevin Spencer

Doesn't matter to me whether you made it up, or what you're quoting. I've
used it before with success. Are you sure you're not talking about a
server-side ASP.Net event?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
K

Kevin Spencer

My apologies, Jon. Apparently, it is browser-dependent. I just tried it on
IE 6 and it behaved as you said - not firing until the texgbox lost focus.
You are correct.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top