Click to See Complete Forum and Search --> : Word Count validation
I have a text field that has to have less than 101 words in it. Not characters but whole words. And i need a check length link like is on the bottom of the page i am entering this message in.
I can do the character count code but how would i go about detecting how many words there are? I think there is a split function which takes a string and turns it into an array based on the delimeter and then i could count the number of elements in the array... but i don' know the keywords etc.. to do it.
Any help is appreciated thanks!:D
Charles
06-27-2003, 04:00 PM
<script type="text/javascript">
<!--
String.prototype.wordCount = function () {return this.split(/\b[\s,\.-:;]*/).length}
alert('Fee, fie, foe, fum. '.wordCount())
// -->
</script>
Hmm i'm not sure how i would implement that. it really doesn't make any sense to me. Care to exaplain it a little bit please?
Thanks!:D
Charles
06-27-2003, 04:26 PM
<script type="text/javascript">
<!--
String.prototype.wordCount = function () {return this.split(/\b[\s,\.-:;]*/).length}
// -->
</script>
<textarea onchange="if (this.value.wordCount() > 100) {alert (['Please limit yourself to 100 words. You see to have', this.value.wordCount(), 'of the little buggers there.'].join(' ')); this.focus()}"></textarea>
Thanks mate, I ended up going with this..
function checkwords(){
var words = document.form.entry.value;
var wordsarray = words.split(" ");
var numwords = wordsarray.length;
alert("Your message is "+numwords+" words long.");
}
Charles
06-27-2003, 05:16 PM
That will count " one , two " as 5 words.
yah it will
but it will count "one, two" as 2 words.
Charles
06-27-2003, 10:15 PM
And
<script type="text/javascript">
<!--
String.prototype.wordCount = function () {return this.split(/\b[\s,\.-:;]*/).length}
// -->
</script>
will get them both right.
ok so the difference is the split delimeter. I couldn't really make since of your code because it wasn't in the format I was going for, but i see now that the split filter is what you ment.
So this is what i have and it works great. thanks!:D
function checkwords(form){
var words = document.form.entry.value;
var wordsarray = words.split(/\b[\s,\.-:;]*/);
var numwords = wordsarray.length;
alert("Your message is "+numwords+" words long.");
}
There is one other question though. I want to be able to put both the form name and textbox name as parameters in the function.
For example
function checkwords(form,textbox){
var words = document.form.textbox.value;
var wordsarray = words.split(/\b[\s,\.-:;]*/);
var numwords = wordsarray.length;
alert("Your message is "+numwords+" words long.");
}
then to use it put
<a href="javascript:checkwords(form1,txtName);">
that doesn't work, so how do you pass 2 paramters to a JS function?
Thanks!:D
Exuro
06-28-2003, 01:31 AM
Originally posted by Arc
then to use it put
<a href="javascript:checkwords(form1,txtName);">
that doesn't work, so how do you pass 2 paramters to a JS function?
There isn't supposed to be a space between java and script in that line of code. Maybe you just typed that in wrong on here, but if not, that may be your problem.
CodeMama
04-01-2009, 03:44 PM
Question about Word Count validations...now that I have it counting the words I need for it to either a.) not allow any typing after the certain count or NOT submit the form if the textarea has too many words.
Suggestions?:confused:
haulin
04-07-2009, 12:44 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>limit words</title>
<script type="text/javascript">
String.prototype.wordCount = function ()
{
var tmp = this.replace(/^\W+/gi, "");
tmp = tmp.replace(/\W+/gi, " ");
return tmp.split(" ").length - 1;
}
function limitWords(who,limit)
{
document.getElementById("my_submit").disabled = (who.value.wordCount() > limit - 1) ? true : false;
}
</script>
</head>
<body>
<form action="#">
<p>
<input id="my_text" onkeyup="limitWords(this,5);" type="text" value="">
<input id="my_submit" type="submit">
</p>
</form>
</body>
</html>
it counts any combination of alphanumeric chars and underscores as a word.
eyemkeith
07-24-2009, 04:36 PM
I love this last form and thanks to all who post it. I'm hoping someone could help me alter it just a bit:
I'm hoping to add to the above script a function that would display in a second textbox a count of the number of words that have been entered into the main text box. Sort of a count up to the number of words that are allowed before the form shuts down like has already been done.
Clear as mud?
here's an example of the type of function i'd like to add to this already great script:
<textarea name="myMessage" onkeyup="wordcount(this.value)"></textarea>
<script language="JavaScript">
var cnt;
function wordcount(count) {
var words = count.split(/\s/);
cnt = words.length;
var ele = document.getElementById('w_count');
ele.value = cnt;
}
document.write("<input type=text id=w_count size=4 readonly>");
</script>
that's what I"m on about. If anyone can put the two together,that would totally rock and i would give you my undying admiration, since i have no money. :)
Thanks much in advance,
Keith.