2

this is the jquery code

var max_count = 200; $(document).ready(function () { var wordCounts = {}; $("#word_count").keyup(function () { var matches = this.value.match(/\b/g); wordCounts[this.id] = matches ? matches.length / 2 : 0; var finalCount = 0; $.each(wordCounts, function (k, v) { finalCount += v; }); var vl = this.value; if (finalCount > max_count) { vl = vl.substring(0, vl.length - 1); this.value = vl; } var countleft = parseInt(max_count - finalCount); $('#display_count').html(finalCount); $('#count_left').html(countleft); am_cal(finalCount); }).keyup(); }); 

The work of this code is to count the number of words and limit them going up 200.

When we type in the textarea it works fine, and it counts and when it reaches 200 it does not allow to write more, but when i copy paste the code, it keeps going above 200, how can i correct this?

here is the fiddle

http://jsfiddle.net/aVd4H/

Thanks in advance

5
  • 3
    your code is not complete in the fiddle, you have missing }); at the end and the function am_cal is missing Commented Sep 9, 2013 at 7:40
  • @ArunPJohny you are right. it is now okay, what about the second part of the question? the main question. Copy pasting going up 200. How can i rectify that? Commented Sep 9, 2013 at 7:43
  • You can run the JSHint function in jsfiddle, it shows you errors. Commented Sep 9, 2013 at 7:44
  • 1
    @neo: You might want to change the title of your question now that your jsFiddle issue is solved. That might avoid more answers asking you to add }); Commented Sep 9, 2013 at 7:45
  • @Harry please answer this now? Commented Sep 9, 2013 at 7:59

2 Answers 2

3

First of all, your code correction is being updated here

and here is a demo for word limit on copy paste you can integrate it in your code.

jQuery(document).ready(function($) { function limits(obj, limit){ var text = $(obj).val(); var length = text.length; if(length > limit){ $(obj).val(text.substr(0,limit)); } else { /** * Alerts the user of the remaining characters. * I do alert here, but you can do any other thing you like. */ alert(limit - length + " characters remaining!"); } } $('textarea').keyup(function() { limits($(this), 20); }); }); 

your complete code according to your requirement HERE

call your am_cal() function at the end.

Sign up to request clarification or add additional context in comments.

5 Comments

@DeepSharma did you updated? where is the final fiddle for word count?
you want to check words as in separated by white space??
@neo your problem is solved check my edit. and accept this as answer if it works for you thanks.
but what do i have to call in am_cal(); ??
Thanks for this, had to add this into a form and was able to go through the examples you posted and edit a little and stick it in.
2
 var max_count = 200; $(document).ready(function () { var wordCounts = {}; $("#word_count").keyup(function () { CheckAndUpdateWordCount(); }); }); 

put your logic in a common method. call that method inside keyup event and afterpaste event.

function CheckAndUpdateWordCount() { var Myelement = $("#word_count"); var matches = Myelement .value.match(/\b/g); wordCounts[Myelement.id] = matches ? matches.length / 2 : 0; var finalCount = 0; $.each(wordCounts, function (k, v) { finalCount += v; }); var vl = Myelement.value; if (finalCount > max_count) { vl = vl.substring(0, vl.length - 1); Myelement.value = vl; } var countleft = parseInt(max_count - finalCount); $('#display_count').html(finalCount); $('#count_left').html(countleft); am_cal(finalCount); } //Custom event After paste $('HTML').live('paste', function (e) { e = $.extend({}, e, { type: 'afterpaste' }); window.setTimeout(function () { $(e.target).trigger(e); }, 0); }); 

Using above method.

$("#word_count").bind('afterpaste', function (e) { //call a method to check //call the method here to check the word count after pasting any content. CheckAndUpdateWordCount(); }); 

1 Comment

anyways, no issue. I have the answer now. Thanks a lot for telling Sudhanshu :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.