/    Sign up×
Community /Pin to ProfileBookmark

Alert on long words in textarea

Hello all,

I’m running a meesage board and sometimes people write gibberish and just hold a key down, churning out 100 or so exclamation marks:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! etc etc etc

It’s annoying and messes up my pages. Does anyone know some javascript code to throw back an alert onsubmit and not send the form through if they have exceeded a maximum number of characters? I’ve tried and failed.

Cheers

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@PittimannApr 19.2004 — Hi!

You can do that before submit:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
else
countfield.value = maxlimit - field.value.length;
}
</script>
</head>
<body>
<center>
<form name=myform action="#" onSubmit="textCounter(document.myform.message,document.myform.remLen,125);">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4 onClick="textCounter(this.form.message,this.form.remLen,125);" onKeyDown="textCounter(this.form.message,this.form.remLen,125);" onKeyUp="textCounter(this.form.message,this.form.remLen,125);" onMouseDown="textCounter(this.form.message,this.form.remLen,125);" onMouseUp="textCounter(this.form.message,this.form.remLen,125);"></textarea>
<br><input type=submit value="Submit">
<input readonly type=text name=remLen size=3 maxlength=3 value=125> characters left</font>
</form>
</center>
</body>
</html>
[/code]

You shouldn't forget to add something which checks the length also server side. The above mentioned example of course won't work, if the user has js disabled.

Cheers - Pit

[COLOR=red]Edit:[/COLOR] I should have used a normal button - sorry...
Copy linkTweet thisAlerts:
@richiebmanauthorApr 19.2004 — Thanks Pittimann. However from reading the script I get the impression it means you can enter 125 characters (or how many I want) and then when you carry on typing, there is no output into the textarea. If that's what happens, then it is a good script I can use, but I need one that checks the text in the textarea and if there are any consecutive strings of characters then it wont be submit. The user would then have to go back to the textarea and put a space in between their unecessary long run of nonsense.

It's not a maxlength of the textarea, just a maxlength of a string of characters that I need.

If I'm wrong about your script, let me know and I'll give it a go. I'm still a novice at javascript.

Cheers
Copy linkTweet thisAlerts:
@PittimannApr 19.2004 — Hi!

Your assumption is completely correct! I thought, that even though you were talking about the long "words", you wanted to limit the entire text area.

Cheers - Pit
Copy linkTweet thisAlerts:
@PittimannApr 19.2004 — Hi!

Try something like this, if you like:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
var words=new Array();
var maxLength=30;
function checkLong() {
words=document.myform.message.value.split(' ');
for (var i=0; i < words.length; i++){
if (words[i].length>maxLength){
alert('Please do not enter words longer than '+maxLength+' characters!');
document.myform.message.focus();
return false;
}
}
return true;
}
</script>
</head>
<body>
<center>
<form name=myform action="#" onSubmit="return checkLong()">
<textarea name=message wrap=physical cols=28 rows=4></textarea>
<br><input type=submit value="Submit">
</form>
</center>
</body>
</html>
[/code]

Cheers - Pit
Copy linkTweet thisAlerts:
@richiebmanauthorApr 20.2004 — Excellent Pittimann, just what I wanted. Will explore server side controls later. I've got the site up now if you want to see your code in practice..............cheap plug of website coming up:

www.bostonwood.co.uk

May see you in Germany Pittiman. My band (who the website is for) is coming to Neuss on friday for a rock festival. Rock on.

And steelersfan88 (if it is the Sheffield Steelers, UK), come down to the Leadmill and see us on Sunday. You might see my brother Paul Jones, who was 3rd in line net minder for the Steelers this season.

It's a small world. And sorry I will never talk about anything other than computer things on this forum, from now on.........I'm just excited.

Cheers,

richiebman
Copy linkTweet thisAlerts:
@steelersfan88Apr 20.2004 — hey, I just get brought up everywhere now. actually, my steelers are from pittsburgh, american football. note the last line in Pit's signature. I'd love to come, but I think it'll cost a lil' bit ? nice talkin to y'all, maybe I'll talk to you later pit ?
Copy linkTweet thisAlerts:
@PittimannApr 20.2004 — Hi!

Just a private thing:

Neuss is just 65 km's (40 miles) from my place. If I manage to go there on Friday (which I doubt), I will try to find you :p!

Cheers - Pit
×

Success!

Help @richiebman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...