Click to See Complete Forum and Search --> : Total chars count for MORE THAN 1 text field


johjon
05-21-2003, 01:46 AM
I have seen code to count the number of chars in 1 text field, BUT is it possible to count (and to set a max limit) to the total numbers of chars in several textfields?

I want the number of chars in 5 textfields not to exceed 160 characters in total (all chars in textfield 1, 2, 3, 4 and 5 should in total not be more than 160 chars). And at the same time see the remaining number of chars that can be entered if possible.

Anyone?

SniperX
05-21-2003, 01:52 AM
You could limit each text field to 30 chars
<input type="text" maxlength="30">
And then use javascript to total the amount of chars in each of the textfields.

i.e.
function totalChars() {
var textOne = document.name.inputOne.value;
var textTwo = document.name.inputTwo.value;
var textThree = document.name.inputThree.value;
var textFour = document.name.inputFour.value;
var textFive = document.name.inputFive.value;
var length=0;

length+=textOne.length;
length+=textTwo.length;
length+=textThree.length;
length+=textFour.length;
length+=textFive.length;
var string="";
string = "Lenth is " + length;
alert(string);
}

I dont know if that is what you are looking for

Gollum
05-21-2003, 02:22 AM
This example shows how it can be done with two text fields - extending it to 5 shouldn't be too hard...

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function Count(oThis)
{
var s1 = document.f.i1.value.toString();
var s2 = document.f.i2.value.toString();

var n = s1.length + s2.length;
var n2 = 160-n;

if ( n2 < 0 )
{
alert('Too Many Characters');
var s3 = oThis.value;
oThis.value = s3.substring(0, s3.length + n2);
n = 160;
n2 = 0;
}

var oSpan = document.getElementById('output');
oSpan.innerHTML = 'n Chars = ' + n.toString() + ', chars to go = ' + n2.toString();
}
</SCRIPT>
</head>
<body>
<form name=f>
<input name=i1 type=text onkeyup="Count(this);"><br>
<input name=i2 type=text onkeyup="Count(this);"><br>
<span id=output></span><br>
</form>
</body>
</html>

johjon
05-21-2003, 05:00 AM
Yes, something like that! Will have a look on it and put in 5 textfields and finetune it.

Thanks!

/Johan