Limit textbox length in words using javascript?

I have the following keyup binding which warns that they have more than 150 characters, but you can just click β€œOK” and continue typing, and then just keep pressing β€œok”.

I want to trim them into 150 words (not characters), and if they type above it, remove the extra features. But I can’t understand how to do this, I can understand the characters. But not words.

jQuery('textarea').keyup(function() { var $this, wordcount; $this = $(this); wordcount = $this.val().split(/\b[\s,\.-:;]*/).length; if (wordcount > 150) { jQuery(".word_count span").text("150"); return alert("You've reached the maximum allowed words."); } else { return jQuery(".word_count span").text(wordcount); } }); 
+7
source share
6 answers

If you want to prevent typing (when counting> 150), you can do the following:

  • Use keypress instead of keyup
  • Instead of return alert() first execute alert() and then return false;

You can also add a change (or blur) event handler to handle text insertion.

 var maxWords = 150; jQuery('textarea').keypress(function() { var $this, wordcount; $this = $(this); wordcount = $this.val().split(/\b[\s,\.-:;]*/).length; if (wordcount > maxWords) { jQuery(".word_count span").text("" + maxWords); alert("You've reached the maximum allowed words."); return false; } else { return jQuery(".word_count span").text(wordcount); } }); jQuery('textarea').change(function() { var words = $(this).val().split(/\b[\s,\.-:;]*/); if (words.length > maxWords) { words.splice(maxWords); $(this).val(words.join("")); alert("You've reached the maximum allowed words. Extra words removed."); } });​ 

Spell here

+4
source
 /** * jQuery.textareaCounter * Version 1.0 * Copyright (c) 2011 c.bavota - http://bavotasan.com * Dual licensed under MIT and GPL. * Date: 10/20/2011 **/ (function($){ $.fn.textareaCounter = function(options) { // setting the defaults // $("textarea").textareaCounter({ limit: 100 }); var defaults = { limit: 100 }; var options = $.extend(defaults, options); // and the plugin begins return this.each(function() { var obj, text, wordcount, limited; obj = $(this); obj.after('<span style="font-size: 11px; clear: both; margin-top: 3px; display: block;" id="counter-text">Max. '+options.limit+' words</span>'); obj.keyup(function() { text = obj.val(); if(text === "") { wordcount = 0; } else { wordcount = $.trim(text).split(" ").length; } if(wordcount > options.limit) { $("#counter-text").html('<span style="color: #DD0000;">0 words left</span>'); limited = $.trim(text).split(" ", options.limit); limited = limited.join(" "); $(this).val(limited); } else { $("#counter-text").html((options.limit - wordcount)+' words left'); } }); }); }; })(jQuery); 

Download this and then you can use the following to make it work:

 $("textarea").textareaCounter({ limit: 100 }); 

http://bavotasan.com/2011/simple-textarea-word-counter-jquery-plugin/

+7
source

Check jQuery: count words in real time and this example: http://jsfiddle.net/gilly3/YJVPZ/1/

Then, if you want to cut out extra words ... you can do something like:

 var maxWords = 10; if(finalCount > maxWords){ $("#a").val(a.value.slice(0,-2)); // the -2 is to remove the extra space at the end }; 

Here is a working example http://jsfiddle.net/YJVPZ/80/

Hope this helps, good luck!

+1
source

Try this feature. The value argument must be your textarea value.

 jQuery('textarea').val(); function wordcount(value) { value = value.replace(/\s+/g," "); var andchr = value.split(" & ").length - 1; var char_count = value.length; var fullStr = value + " "; //word count for regional language v = value.split(' '); var word_count1 = v.length; var cheArr = Array('@','.','"',"'",'_','-','+','=',';','&','*','\(','\)','{','}','[','}','|','\\','\,','/'); for(i=0; i<=cheArr.length; i++) { word_count1 = word_count1 + value.split(cheArr[i]).length - 1; } //word count for all languages var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi; var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, ""); var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi; var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " "); var splitString = cleanedStr.split(" "); var word_count = (splitString.length - 1) + andchr; if(word_count1 > word_count) { word_count = word_count1; } if(value == '' || value == null || typeof(value) == 'undefined'){ word_count = 0; } alert(word_count); } 
0
source
 $("textarea").keyup(function(){ var obj = $(this); var maxLen = 150; var val = obj.val(); var chars = val.length; if(chars > maxLen){ obj.val(val.substring(0,maxLen)); } }); 
0
source

Record these events:

 $('textarea').on('paste cut keydown', function(){...}); 
-one
source

All Articles