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);
$this->setName($f);
list($rows, $cols) = $f ? explode('x',$f) : array(10,50);
$this->rows = intval($rows);
$this->cols = intval($cols);
$this->value = $f;
}

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.

javacript code

<script language="javascript" type="text/javascript">
			function limitText(limitField, limitCount, limitNum) {
				if (limitField.value.length > limitNum) {
					limitField.value = limitField.value.substring(0, limitNum);
				} else {
					limitCount.value = limitNum - limitField.value.length;
				}
			}
			</script>

Form Field and countdown

<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>
<span class="limit">(Maximum characters: <input name="countdown" type="text" value="150" size="1" readonly>)

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:

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

<script language=JavaScript>
<!--
function check_length(my_form)
{
maxLen = 400; // 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=8 cols=60></textarea>
<br>
<input size=1 value=400 name=text_num readonly> Characters Left
</form>

</body>
</html>

To have a html script in a php file simply place the html code outside the php brackets.

I also have similar code. This is very good. Try it.

<script language = "Javascript">
maxL=400;
var bName = navigator.appName;
function taLimit(taObj) {
	if (taObj.value.length==maxL) return false;
	return true;
}

function taCount(taObj,Cnt) {
	objCnt=createObject(Cnt);
	objVal=taObj.value;
	if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
	if (objCnt) {
		if(bName == "Netscape"){	
			objCnt.textContent=maxL-objVal.length;}
		else{objCnt.innerText=maxL-objVal.length;}
	}
	return true;
}
function createObject(objId) {
	if (document.getElementById) return document.getElementById(objId);
	else if (document.layers) return eval("document." + objId);
	else if (document.all) return eval("document.all." + objId);
	else return eval("document." + objId);
}
</script>

 <form name="myfrm" id="myfrm" method="post">

 <textarea cols="60" rows="3" name="short_desc" onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')"> </textarea>

<font color="#006600">Max. Length: 
				<span  id="myCounter">400</span>	
				</font>
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.