Count and limit the number of words in a textbox

Posted on 2006-05-22
Last Modified: 2012-05-05
I need to limted the number of words a user can enter into a textbox on an webform.  I suppose that the words need to be counted as the user is entering them and then stopped when they reach the maximum word count.  Does anybody have any examples of how to do this?

Any help would be appreciated.

Question by:Mike_Stevens
    LVL 3

    Accepted Solution

    to limit the max number of characters in a textbox you can use a regularexpressionvalidator with validationexpression="^[S\s\]{1,200}$"

    this would limit the texbox to 200 characters. (I use this)

    you might try something like
    "^[\b(\w+?)\b]{1,10}$"  would allow 10 words

    LVL 7

    Assisted Solution

    Get the no of words by spliting the text by space. and check the length of the words with the max words allowed. Please check the code below..

    <script language="javascript">
    function CheckLength(value,MaxCount)
    value = value.replace("\s\s"," ");
    var words = value.split(" ");

       return false;
       return true;

    function txtValue_KeyPress()
          var value = document.getElementById("txtvalue").value;
          return CheckLength(value,15);

    document.getElementById("txtvalue").onkeypress = txtValue_KeyPress;


    Hope this helps you,
    LVL 4

    Assisted Solution


    why dont u use the maxlength property of text boxes which will do exactly the same as yr requirement


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now