Word count?

P

Paul M

Hi
I have a text entry box and want to set validation rules.I can see min and
max char but I really want to set a word count.
How can I achieve this.
Paul M
 
K

Kevin Spencer

Why would you want a word count? Generally, validation is for the purpose of
making sure that no exceptions occur when the form is processed. If, for
example, you're putting data into a database, you might want to restrict the
size of the input to the size of the column it goes in.

It can be done, but it's a bit tricky, and requires JavaScript. Basically,
you count the spaces, and add 1.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
J

Jim Buyens

Add this script anywhere in the page (i.e. in the <head> section):

<script>
function chkWordCnt(tbox)
{
// eliminate multiple white spaces
// eliminate leading and trailing white spaces
tbox.value = tbox.value.replace(/\s+/g," ");
tbox.value = tbox.value.replace(/(^\s+)|(\s+$)/g,"") ;

words = tbox.value.split(" ");
if ((words.length < 2) || (words.length > 4))
{
alert("You must specify two to four words.")
tbox.focus();
}
}
</script>

and then add the following attribute to your text box:

<input type="text" onblur="chkWordCnt(this);" ...>

As stated, this demands two to four words. If you want a different number of
words, update the "if" and "alert" statements accordingly.

If the box shouldn't be blank, check that using standard FrontPage form
field validation.

Jim Buyens
Microsoft MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Windows SharePoint Services Inside Out
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------
 
Top