jasmeet Posted August 8, 2013 Share Posted August 8, 2013 hi, count number of words in textbox and then validate that the number of words should be between 400 to 1000. i googled alot... but didn't get an correct answer.... ??? need help!!! with regards, jasmeet. Link to comment https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/ Share on other sites More sharing options...
jasmeet Posted August 8, 2013 Author Share Posted August 8, 2013 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> <script> $(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');} </script> but i dnt know how to validate.....?? thanks. Link to comment https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/#findComment-1443971 Share on other sites More sharing options...
codefossa Posted August 8, 2013 Share Posted August 8, 2013 Demo: http://lightron.tk/tmp/53/ HTML <textarea id="input"></textarea> <div id="result">0 Words</div> Javascript // Wait for the page to load. $(document).ready(function() { // Define some default text. var defaultText = "Enter some text to test. (:"; // Set default text on page load and lighten text color. $("#input") .val(defaultText) .css("color", "#555"); // When you focus, remove default text and darken text. $("#input").focus(function() { if ($(this).val() == defaultText) $(this) .val("") .css("color", "#000"); }); // When they let go of a key .. $("#input").keyup(function() { // 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"); else $("#result").css("color", "#C00"); }); }); Link to comment https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/#findComment-1443984 Share on other sites More sharing options...
Adam Posted August 9, 2013 Share Posted August 9, 2013 Put simply: str.split(' ').length;That will return you the word count. Link to comment https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/#findComment-1444091 Share on other sites More sharing options...
codefossa Posted August 12, 2013 Share Posted August 12, 2013 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 https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/#findComment-1444544 Share on other sites More sharing options...
Adam Posted August 12, 2013 Share Posted August 12, 2013 Yeah that's a fair point. I was just highlighting the actual code for it, in-case the OP was lost in the rest. Link to comment https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/#findComment-1444583 Share on other sites More sharing options...
codefossa Posted August 12, 2013 Share Posted August 12, 2013 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. str.split(/\s+[^$]/).length 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. Link to comment https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/#findComment-1444645 Share on other sites More sharing options...
kicken Posted August 12, 2013 Share Posted August 12, 2013 I'd just trim the value first to clear leading/trailing spaces. val = val.replace(/^\s+|\s+$/, ''); var words = val==''?0:val.split(/\s+/).length; Link to comment https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/#findComment-1444649 Share on other sites More sharing options...
jasmeet Posted August 17, 2013 Author Share Posted August 17, 2013 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 https://forums.phpfreaks.com/topic/280955-count-words-in-textbox-and-validate/#findComment-1445486 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.