PDA

View Full Version : The number of characters in a textbox


101
10-13-2002, 03:56 PM
Can any one help me make a text box and at the side it has a number. The should number increses everytime you add another character to the text box (So it will show the number of charcters in that box)

I have made some code but you have to click for the number of characters in the text box.



<html>
<head>
<title>
</title>
</head>
<body>

<script language="JavaScript">

function CheckSize(form, field) {
if (field ==1) {
Ctrl = form.test;
y = 30;
x = Ctrl.value.length;
}
if (x < y)
SendMsg (Ctrl, "Number of characters is: " + x); else
SendMsg (Ctrl, "Too Long. Number of characters is: " + x);}

function SendMsg (Ctrl, PromptStr) { document.write (PromptStr);
Ctrl.focus(); return;}

</script>

<span id=layer1>0</span>

<form>
<table border="1" cellspacing="1" width="500" cellpadding="4">
<tr>
<td width="100%"><textarea wrap="virtual" rows="3" name="test" cols="26"></textarea>
<input onclick="CheckSize(this.form,1)" type="button" value="Count"></td>
</tr>
</table>
</form>

</body>
</html>

A1ien51
10-13-2002, 05:07 PM
Here is an idea:

<html>
<head>
<script>
function CheckIt(WElem,DLimit){
DWords=document.TA["T"+WElem].value;
if(DWords.length>=DLimit){
DWords=DWords.slice(0,DLimit);
document.TA["T"+WElem].value=DWords;
}
Mess=DWords.length+"/"+DLimit;
if (document.getElementById) {document.getElementById("B"+WElem).innerHTML = Mess}
else if (document.all) {document.all["B"+WElem].innerHTML = Mess}
else if (document.layers) {document.layers["B"+WElem].innerHTML = Mess}
else{}
}
</script>
</head>
<body>
<form name="TA">
<textarea name="T1" rows="6" cols="20" onkeydown="CheckIt(1,100)" onkeyup="CheckIt(1,100)" onblur="CheckIt(1,100)" ></textarea>
<span id="B1">0/100</span>
<br>
<textarea name="T2" rows="4" cols="20" onkeydown="CheckIt(2,70)" onkeyup="CheckIt(2,70)" onblur="CheckIt(2,70)" ></textarea>
<span id="B2">0/70</span>
<br>
<input type="text" name="T3" size="15" onkeydown="CheckIt(3,10)" onkeyup="CheckIt(3,10)" onblur="CheckIt(3,10)" >
<span id="B3">0/10</span>

</form>
</body>
</html>

chrismiceli
10-13-2002, 07:46 PM
this is what you want right?

function test() {
hi = document.formanme.textname.lenght
documetn.write(hi)
}


<input type="text" name="textname" value="" onChange="test()">

101
10-13-2002, 08:07 PM
Thanks alot both of you!

beetle
10-13-2002, 08:16 PM
For what it's worth...

http://www.dynamicdrive.com/dynamicindex16/limitinput.htm

whammy
10-14-2002, 05:06 AM
http://www.solidscripts.com/solidscripts_new/displayscript.asp?sid=1

If I do say so myself, this also covers a lot of cross-browser problems the other scripts don't, such as in Opera and NS 4.x... and in IE and other modern browsers, it just doesn't let you type any further... (but if you copy/paste in any of the browsers I've mentioned, it still trims the entry and warns the user).

:)