Page 1 of 1

[SOLVED]Textarea and limiting the input

Posted: Wed Jun 23, 2004 3:27 am
by Calimero
I have a textarea (not a textfield) and I would need a code how to limit the amount of data submited trough it.

Any idea is appreciated.

My idea was to try to limit it by the number of characters because although the VARCHAR in the db can do the job for 255 characters, i can't protect blob types of fields. left the visitor space to write a lot of the text - but now I have a problem how to prevent manipulative actions.

Method by the number of words wouldn't do, jghsldhglsdfhgjkdsklfghksjdhfgjkhsdfgklshgjshlfgjkhslkdfglskdhfglk
<< this is still one word :).

Because I have numerous textareas is there a code to write it once, and for it to be called upon when it's needed.

Thanks ahead!

Posted: Wed Jun 23, 2004 3:33 am
by feyd
[php_man]strlen[/php_man] if you want to know the length.. to grab the first 255 characters of any string:
untested

Code: Select all

substr($string,0,255);
// or
preg_match('#^(.{0,255})#',$string,$matches);
echo 'first '.strlen($matches[1]).' characters are: '.$matches[0];

Posted: Wed Jun 23, 2004 8:05 am
by Grim...
Or you can do it better (from a user point of view) with JScript.

Code: Select all

<script language = "Javascript">
function taLimit() &#123;
    var taObj=event.srcElement;
    if (taObj.value.length==taObj.maxLength*1) return false;
&#125;

function taCount(visCnt) &#123;
    var taObj=event.srcElement;
    if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
    if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
&#125;
</script>

<form method="post" action="page.php">
<TEXTAREA onkeypress="return taLimit()" onkeyup="return taCount(myCounter)"
name="text" rows=5 wrap=physical cols=25 maxLength="255"></TEXTAREA></textarea><br>
You have <B><SPAN id=myCounter>255</SPAN></b> characters remaining...<br><br>
<input type="submit" value="Submit">
</form>
Gives the user a counter and everything.