Jump to content

count words in textbox and validate

Go to solution Solved by jasmeet,

Recommended Posts

even i just want to validate textbox that number of words should be between 400 to 1000.

i know very less about javascript. hope you understand


this is the codfe which i use for counting

<form><div id="example1_count" style="display:none"></div><textarea id="example1" rows="5" cols="50" class="word_count">a b c</textarea></form>


$(document).ready(function() {    $('.word_count').each(function() {        var input = '#' + this.id;        var count = input + '_count';        $(count).show();        word_count(input, count);        $(this).keyup(function() { word_count(input, count) });    });});function word_count(field, count) {    var number = 0;    var matches = $(field).val().match(/\b/g);    if(matches) {        number = matches.length/2;    }    $(count).text( number + ' word' + (number != 1 ? 's' : '') + ' approx');}


but i dnt know how to validate.....??



Edited by jasmeet
Link to comment
Share on other sites

Demo: http://lightron.tk/tmp/53/




<textarea id="input"></textarea>
<div id="result">0 Words</div>




// Wait for the page to load.
    // Define some default text.
    var defaultText = "Enter some text to test. (:";
    // Set default text on page load and lighten text color.
        .css("color", "#555");
    // When you focus, remove default text and darken text.
        if ($(this).val() == defaultText)
                .css("color", "#000");
    // When they let go of a key ..
        // Grab the current value.
        var val = $(this).val();
        // Split by spaces and count the words.
        var words = val.replace(/\s+/, '') == "" ? 0 : val.split(/\s+[^$]/).length;
        // Show number of words.
        $("#result").html(words + " Words");
        // Color green if valid, red if invalid number of words.
        if (words >= 400 && words <= 1000)
            $("#result").css("color", "#0C0");
            $("#result").css("color", "#C00");
Link to comment
Share on other sites

Unless they use double spaces at any time, usually after a period.  They may have a space(s) at the end of the string, or for some reason indent with spaces.  It just wouldn't return accurately for a number of reasons, which you should plan for if you're using it publicly.

Link to comment
Share on other sites

For that I would give what I used in the example, or something similar.  The only problem is without the check for characters, it will show 1 word for an empty string.


Split by spaces (no matter how many are together) as long as they're not at the end of the string.  You could probably add tabs and whatnot in there as well, but for regular word counting, that should suffice.


Edit: Oops, that checks for a physical "$".  Hmm .. maybe /\s+[^\s]/ would be better?  My example only worked correctly because there's no character after spaces at the end unless you put the $, then you see the problem.


"This $would fail."


Maybe you got a better idea?  Not sure OP has checked back though.

Edited by Xaotique
Link to comment
Share on other sites

  • Solution
i use this code for validating it.. and it works...



var age = document.getElementById("word_count").value;

if(age < 400){

alert("Please enter more than 400 words.");

  return false;


if(age > 3000){

alert("Please enter less than 3000 words.");

  return false;


  return true;



thanks guys for replying.... 

thankyou very much... :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.