justmelat
asked on
How do I add ability to count characters in textarea using this code(php, javascript)?
ok here is difficult one
i have inherited code that I have to modify
it's a web app using php, mysql. below are a couple of the labels and textareas that appears
my boss want to add the feature that counts down the number of characters used in the text box.
I found the code that does exactly what I want (attached) but because of the way the original programmer set up his form fields i don't see how to do this.
<tr>
<!-- Objective -->
<td><label><?=$Q['Q_9']?>< /label></t d>
<td><?=inputOrText($mode," textarea", $A['Q_9'], array("nam e"=>"Q_9", "class"=>" regular"," rows"=>"4" ,"cols"=>" 70"))?></t d>
</tr>
<tr>
<!-- Goals -->
<td valign='top'><label><?=$Q[ 'Q_10']?>< /label></t d>
<td><?=inputOrText($mode," textarea", $A['Q_10'] ,array("na me"=>"Q_10 ","class"= >"regular" ,"rows"=>" 4","cols"= >"70"))?>< /td>
</tr>
<tr>
i have inherited code that I have to modify
it's a web app using php, mysql. below are a couple of the labels and textareas that appears
my boss want to add the feature that counts down the number of characters used in the text box.
I found the code that does exactly what I want (attached) but because of the way the original programmer set up his form fields i don't see how to do this.
<tr>
<!-- Objective -->
<td><label><?=$Q['Q_9']?><
<td><?=inputOrText($mode,"
</tr>
<tr>
<!-- Goals -->
<td valign='top'><label><?=$Q[
<td><?=inputOrText($mode,"
</tr>
<tr>
function check_length(my_form)
{
maxLen = 50; // max number of characters allowed
if (my_form.my_text.value.length >= maxLen) {
// Alert message if maximum limit is reached.
// If required Alert can be removed.
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
// Reached the Maximum length so trim the textarea
my_form.my_text.value = my_form.my_text.value.substring(0, maxLen);
}
else{ // Maximum length not reached so update the value of my_text counter
my_form.text_num.value = maxLen - my_form.my_text.value.length;}
}
//-->
</script>
</head>
<body>
<form name=my_form method=post>
<textarea onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=my_text rows=4 cols=30></textarea>
<br>
<input size=1 value=50 name=text_num> Characters Left
</form>
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
What is the output from the php at this stage?
ASKER
it displays the textarea and the text box containing the character max, but when i type in the text box
the character number does not decrease.
the character number does not decrease.
What happens when you focus to another field? Does something happen?
Can you do view source and see if the onchange is added to the html?
Can you do view source and see if the onchange is added to the html?
ASKER
ok Roonaan:
i knew i'd messed up something in my script. I went back over it made the following changes. now the problem is, the character count does change, but the numbers are not counting down, but up and they are get larger and larger even after i have deleted all the characters trying to start over.
<script language=JavaScript>
<!--
function check_length(my_form)
{
maxLen = 50; // max number of characters allowed
var f = document.forms.intake;
if (my_form.Q_9.value.length >= maxLen) {
// Alert message if maximum limit is reached.
// If required Alert can be removed.
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
// Reached the Maximum length so trim the textarea
my_form.Q_9.value = my_form.Q_9.value.substrin g(0, maxLen);
}
else{ // Maximum length not reached so update the value of my_text counter
my_form.text_num.value = maxLen - my_form.Q_9.value.length;}
}
//-->
</script>
i knew i'd messed up something in my script. I went back over it made the following changes. now the problem is, the character count does change, but the numbers are not counting down, but up and they are get larger and larger even after i have deleted all the characters trying to start over.
<script language=JavaScript>
<!--
function check_length(my_form)
{
maxLen = 50; // max number of characters allowed
var f = document.forms.intake;
if (my_form.Q_9.value.length >= maxLen) {
// Alert message if maximum limit is reached.
// If required Alert can be removed.
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
// Reached the Maximum length so trim the textarea
my_form.Q_9.value = my_form.Q_9.value.substrin
}
else{ // Maximum length not reached so update the value of my_text counter
my_form.text_num.value = maxLen - my_form.Q_9.value.length;}
}
//-->
</script>
ASKER
ok Roonaan:
i think we've got it. I changed your onChange to onkeypress and it seems to work, one question:
in the code i found they have this:
onKeyPress=check_length(th is.form); onKeyDown=check_length(thi s.form);
I am not using the onKeyDown, should I include it?
i think we've got it. I changed your onChange to onkeypress and it seems to work, one question:
in the code i found they have this:
onKeyPress=check_length(th
I am not using the onKeyDown, should I include it?
There is nothing in the code that should explain the behavior you describe.
I cannot even rewrite the code to do something else at this stage.
I cannot even rewrite the code to do something else at this stage.
ASKER
I did the ususal and closed the browser and reopened. It seems to be fine now.
one last question. If i need two or three of these on a page. What do I need to change?
one last question. If i need two or three of these on a page. What do I need to change?
You can include the onKeyDown as well, it doesn't break anything, so if it adds something its worth adding.
I would use something like:
<script language=JavaScript>
<!--
function check_length_for_field(my_ field, my_label)
{
maxLen = 50; // max number of characters allowed
if (my_field.value.length >= maxLen) {
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
// Reached the Maximum length so trim the textarea
my_field.value = my_field.value.substring(0 , maxLen);
}
my_label.value = maxLen - my_field.value.length;}
//-->
</script>
Then use
"onkeypress" => 'check_length_for_field(th is, this.form.text_num_1)
"onkeypress" => 'check_length_for_field(th is, this.form.text_num_2)
"onkeypress" => 'check_length_for_field(th is, this.form.text_num_3)
Wherer the text_num_## is a input field you add per textarea. So textarea Q9 has text_num_Q9 etc.
<script language=JavaScript>
<!--
function check_length_for_field(my_
{
maxLen = 50; // max number of characters allowed
if (my_field.value.length >= maxLen) {
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
// Reached the Maximum length so trim the textarea
my_field.value = my_field.value.substring(0
}
my_label.value = maxLen - my_field.value.length;}
//-->
</script>
Then use
"onkeypress" => 'check_length_for_field(th
"onkeypress" => 'check_length_for_field(th
"onkeypress" => 'check_length_for_field(th
Wherer the text_num_## is a input field you add per textarea. So textarea Q9 has text_num_Q9 etc.
ASKER
Ok, will do, Thanks so much. You are a complete Genius!!!!!!!!!!!!!!!!!!!! !!!
ASKER
Pure Genius
ASKER
here is what I added/changed:
<td><?=inputOrText($mode,"
<br><input size=1 value=50 name=text_num> Characters Left</td>
=================The Function==================
<script language=JavaScript>
<!--
function check_length(my_form)
{
var f = document.forms.ChareForm;
maxLen = 50; // max number of characters allowed
if (f.Q_9.value.length >= maxLen) {
// Alert message if maximum limit is reached.
// If required Alert can be removed.
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
// Reached the Maximum length so trim the textarea
f.Q_9.value = f.Q_9.value.substring(0, maxLen);
}
else{ // Maximum length not reached so update the value of my_text counter
f.Q_9.value = maxLen - f.Q_9.value.length;}
}
//-->
</script>