Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

textarea box character restriction

Quote Reply
textarea box character restriction
Hi,

This is a simple little plugin, not my own work it is available on the web but I thought it might be useful to anyone who want to restrict the amount of characters that a user who uses the submission form can enter into a text area box.

The reaosn for this is you can say "brief description - 25 words max "
but you can be sure if they can add a million words they will in the hope that you won't see it and they get more keywords to give them a ranking advantage.

I searched for this cause it's a pain tidying up after them and editing submissions and even if you do they can simply log in and edit the text area box again.

All this script does is the same as maxlength

For single boxes in a form nothing special needed, you would just use after size, maxlength=30
e.g size="30" maxlength=25
note no "" around the number for maxlength

In a single box this restricts the max number of characters to 25

For text area boxes this doesn't apply, it just doesn't work so you need to use a bit of script to do the same job.

Create a global in the build>template globals menu bit called text_area_script

and add this:
<SCRIPT>
function checkMaxLength (textarea, evt, maxLength) {
if (textarea.selected && evt.shiftKey)
// ignore shift click for select
return true;
var allowKey = false;
if (textarea.selected && textarea.selectedLength > 0)
allowKey = true;
else {
var keyCode =
document.layers ? evt.which : evt.keyCode;
if (keyCode < 32 && keyCode != 13)
allowKey = true;
else
allowKey = textarea.value.length < maxLength;
}
textarea.selected = false;
return allowKey;
}
function storeSelection (field) {
if (document.all) {
field.selected = true;
field.selectedLength =
field.createTextRange ?
document.selection.createRange().text.length : 1;
}
}
</SCRIPT>

and then create another global called text-area_call and save to this the following

ONKEYDOWN="return checkMaxLength(this, event, 200)" ONSELECT="storeSelection(this)"

The 200 restricts the text area to 200 characters, it can be any number but basically if a word is on average 7 characters long then 200 lets you enter around 28 words max

To make it work-
on your submit (or any other you decide to use it on) form include the global
<%text_area_script%>
pref in your header if you have one but it doesn't really matter

Then in your submit box text area bit of code insert after the cols= bit <text_area_call%>
e.g
<textarea wrap="virtual" name="Description" rows="3" cols="42" <%text_area_call%>><%if Description%><%Description%><%endif%></textarea>

In your html page editor preview view it might display an extra '<' in the box but it is ok, the program gets confused with all the ><<>> going on.

And thats it, now your users can only enter sensible legth descriptions, you don't have to keep stripping them down and editing, results will still be relevant and your output in Links SQL isn't clogged. Easy life! Smile

Hope that helps!
KevM

Cheers
KevM