Textarea characters limit

Reply

Join Date: May 2009
Posts: 1
Reputation: Wallk is an unknown quantity at this point 
Solved Threads: 0
Wallk Wallk is offline Offline
Newbie Poster

Textarea characters limit

 
0
  #1
May 3rd, 2009
Hi,

I want to limit the characters in the textarea to 400...

i found this script but i dont know how to implement in my script...

this is the script:

<html><head>
<title>(Type a title for your page here)</title>

<script language=JavaScript>
<!--
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>

</body>
</html>


and my script is:

<?php

class FormTextarea extends FormElement {

var $cols;
var $rows;
var $value;

function FormTextarea($f) {
$this->setCaption($f['caption']);
$this->setName($f['name']);
list($rows, $cols) = $f['size'] ? explode('x',$f['size']) : array(10,50);
$this->rows = intval($rows);
$this->cols = intval($cols);
$this->value = $f['value'];
}

function getRows() {
return $this->rows;
}

function getCols() {
return $this->cols;
}

function getValue() {
return $this->value;
}

function render() {
return '<textarea class="fields" name="'.$this->getName().'" id="'.$this->getName().'" rows="'.$this->getRows().'" cols="'.$this->getCols().'"'.$this->getExtra().'>'.$this->getValue().'</textarea>';
}

}

?>



Please help me with this... i really do not know php.

English is not my language... so sorry for mistakes.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: timmah is an unknown quantity at this point 
Solved Threads: 1
timmah timmah is offline Offline
Newbie Poster

Re: Textarea characters limit

 
0
  #2
May 3rd, 2009
javacript code
  1. <script language="javascript" type="text/javascript">
  2. function limitText(limitField, limitCount, limitNum) {
  3. if (limitField.value.length > limitNum) {
  4. limitField.value = limitField.value.substring(0, limitNum);
  5. } else {
  6. limitCount.value = limitNum - limitField.value.length;
  7. }
  8. }
  9. </script>

Form Field and countdown

  1. <textarea name="limitedtextfield" class="where" onkeydown="limitText(this.form.limitedtextfield,this.form.countdown,150);" onkeyup="limitText(this.form.limitedtextfield,this.form.countdown,150);"><?=$_REQUEST['limitedtextfield'];?></textarea>
  2. <span class="limit">(Maximum characters: <input name="countdown" type="text" value="150" size="1" readonly>)
If you at first you don't succeed, quickly deny you were even trying!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,514
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Posting Virtuoso

Re: Textarea characters limit

 
0
  #3
May 3rd, 2009
Please help me with this... i really do not know php.
If you are new to php then I would suggest to start with basic php and not oop (Object Orientated Programing) as the concept of oop for a beginner can be hard to get a grip of. Also I can't see much functionality in your php script so you could replace your php script with the following html script:
  1. <html><head>
  2. <title>(Type a title for your page here)</title>
  3.  
  4. <script language=JavaScript>
  5. <!--
  6. function check_length(my_form)
  7. {
  8. maxLen = 400; // max number of characters allowed
  9. if (my_form.my_text.value.length >
  10. = maxLen) {
  11. // Alert message if maximum limit is reached.
  12. // If required Alert can be removed.
  13. var msg = "You have reached your maximum limit of characters allowed";
  14. alert(msg);
  15. // Reached the Maximum length so trim the textarea
  16. my_form.my_text.value = my_form.my_text.value.substring(0, maxLen);
  17. }
  18. else{ // Maximum length not reached so update the value of my_text counter
  19. my_form.text_num.value = maxLen - my_form.my_text.value.length;}
  20. }
  21. //-->
  22. </script>
  23.  
  24. </head>
  25.  
  26. <body>
  27. <form name=my_form method=post>
  28. <textarea onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=my_text rows=8 cols=60></textarea>
  29. <br>
  30. <input size=1 value=400 name=text_num readonly> Characters Left
  31. </form>
  32.  
  33. </body>
  34. </html>
To have a html script in a php file simply place the html code outside the php brackets.
Last edited by cwarn23; May 3rd, 2009 at 9:38 pm.
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - MacGyver Fan
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 257
Reputation: BzzBee is an unknown quantity at this point 
Solved Threads: 37
BzzBee BzzBee is offline Offline
Posting Whiz in Training

Re: Textarea characters limit

 
0
  #4
May 4th, 2009
I also have similar code. This is very good. Try it.

  1. <script language = "Javascript">
  2. maxL=400;
  3. var bName = navigator.appName;
  4. function taLimit(taObj) {
  5. if (taObj.value.length==maxL) return false;
  6. return true;
  7. }
  8.  
  9. function taCount(taObj,Cnt) {
  10. objCnt=createObject(Cnt);
  11. objVal=taObj.value;
  12. if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
  13. if (objCnt) {
  14. if(bName == "Netscape"){
  15. objCnt.textContent=maxL-objVal.length;}
  16. else{objCnt.innerText=maxL-objVal.length;}
  17. }
  18. return true;
  19. }
  20. function createObject(objId) {
  21. if (document.getElementById) return document.getElementById(objId);
  22. else if (document.layers) return eval("document." + objId);
  23. else if (document.all) return eval("document.all." + objId);
  24. else return eval("document." + objId);
  25. }
  26. </script>
  27.  
  28. <form name="myfrm" id="myfrm" method="post">
  29.  
  30. <textarea cols="60" rows="3" name="short_desc" onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')"> </textarea>
  31.  
  32. <font color="#006600">Max. Length:
  33. <span id="myCounter">400</span>
  34. </font>
  35. </form>
Last edited by BzzBee; May 4th, 2009 at 12:37 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC