javascript - add validation and not allow form submit with jQuery word count -


i have text area in can have no more 100 words. found nice indicator script counts down words on page, still allows user submit if users typed in more 100 words. need integrate validation within. note: using validate.js in other places.

$(document).ready(function() {     $("#word_count").on('keyup', function() {         var words = this.value.match(/\s+/g).length;         if (words > 100) {             // split string on first 100 words , rejoin on spaces             var trimmed = $(this).val().split(/\s+/, 100).join(" ");             // add space @ end keep new typing making new words             $(this).val(trimmed + " ");         }         else {             $('#display_count').text(words);             $('#word_left').text(100-words);         }     });  });   

a quick , dirty hack grab text $('#display_count'), convert number , see if it's more or equal 100 , stop form submitting. since you're using jquery validation plugin (assumed), do:

$(".myform").validate({   submithandler: function(form) {       var total = parseint($('#display_count').text(), 10);       if ( total >= 100 ) {           alert ("max limit reached");           return;       }       $(form).submit();   } }); 

more on this fiddle


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -