/    Sign up×
Community /Pin to ProfileBookmark

Minimum Word Count Validation for Forms

Hello, curently I am trying to figure out this script: [url]http://javascript.internet.com/forms/val-no-alert.html[/url]. It is a script for validating the fields in a contact form. If information isn’t entered into a certain field, a bright red error message will appear below the field.

And the part of the script that I am having trouble with is the one below (I modified it a bit from the original code to suit my preferences).

[code=php](f.paragraph.value.length < 50)[/code]

The 50 is the minimum amount of characters that must be entered into the form before submission. But I don’t want it to count characters. Instead, I would like it to count the number of words. I have tried copying and altering the e-mail validation code so instead of detecting a “@”, it would detect a certain number of ” “, like so:

[code=php](f.paragraph.value.search(” “) <= 50 || f.paragraph.value.search(“[.*]”) <= 50) // validate word count[/code]

I guess the space made it undefined, because it didn’t work (the error message would still be there and the form wouldn’t send). I’ve tried using [b]&nbsp;[/b] to define the ” “, but that didn’t work either. I’ve also spent several hours on Google looking for a “minimum word count” script. I found a guestbook script that featured it and also a forum hack, but those scripts are very different from the script I am currently using and I am not a professional scripter, so it is hard for me to spot the parts of the scripts needed for a minimum word count.

I could not find any solutions here in the forums, either. I’m getting tired of looking for scripts and trying out different kinds of commands, so I am looking to the people of this forum as a last resort. Would please you help me? I would be very greatful. If it is too confusing, then I can try to explain it a bit more. If it’s impossible to code, then I’ll just let it be.

Anyways, thank you for taking time to read all of this ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@NedalsJan 13.2006 — Here's a method that will work as long as there is only one space between the words
<i>
</i>var text = "the quick brown fox jumps over the lazy dog";
var len = (text.split(" ")).length;
alert(len);
Copy linkTweet thisAlerts:
@CrazyMerlinJan 13.2006 — you could add a small for loop to check that there is only 1 space between each word

what you are asking is far from impossible. It is just simple form validation.

explain in a little more detail about what fields you have and what they are supposed to be and I'll put you a script together
Copy linkTweet thisAlerts:
@JenlauthorJan 13.2006 — [b]Nedals:[/b] How would I use that? ^^; Sorry, I'm not very Javascript-literate.

[b]CrazyMerlin:[/b] I knew there's a way to do it, but I just don't know how to ?

Well, the fields are for a little contest submission form for my site. I start a story, and people will continue it by sending in their own submissions. I just need their name, e-mail. and a short paragraph that continues the story. I wanted to add in a minimum word count to reduce the spamming and also to look for good submissions (not stuff like "Bob said, 'Hi.'").

If you are willing to script that for me, that'd be awesome ? Will it still contain the error notifications (for fields left blank/not enough words) that was in the original script?
Copy linkTweet thisAlerts:
@CharlesJan 13.2006 — &lt;textarea onchange="if (this.value.match (/S+/g).length &lt; 3) {alert ('Three words or more, please.'); this.value=''; this.focus()}"&gt;&lt;/textarea&gt;

[i]// or[/i]

&lt;textarea onchange="if (this.value.split (/s/).length &lt; 3) {alert ('Three words or more, please.'); this.value=''; this.focus()}"&gt;&lt;/textarea&gt;
×

Success!

Help @Jenl 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 5.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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