[SOLVED]Textarea and limiting the input

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

[SOLVED]Textarea and limiting the input

Post 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!
Last edited by Calimero on Fri Jun 25, 2004 5:42 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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];
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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.
Post Reply