PASTE AWARE WORD COUNTER WITH WORD LIMITING FOR FORM TEXTAREA FIELDS - Dec 13th, 2018


Here’s a simple to implement word counter to use when you want to limit the length of the input to a front end html
form textarea field.

In the head of the web page insert the following:
(Change the number of words to suit your needs)


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript"><!--

$(document).ready(function() {
$("#word_count").on('keyup', function() {
var words = this.value.match(/\S+/g).length;

if (words > 200) {
// Split the string on first 200 words and rejoin on spaces
var trimmed = $(this).val().split(/\s+/, 200).join(" ");
// Add a space at the end to make sure more typing creates new words
$(this).val(trimmed + " ");
}
else {
$('#display_count').text(words);
$('#word_left').text(200-words);
}
});
});
// --></script>


Then, for the textarea field in the form, use something like:


<textarea name="your_name" id="word_count" COLS=50 ROWS=8></textarea>

Total word Count : <span id="display_count">0</span> words. Words left : <span
id="word_left">200</span>
If you paste more than 200 words your entry will be truncated.



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service