PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Maximum Word Count

  1. Rothbert
    Member

    A maximum word count feature would be great.

    I know there is maximum character count available, but word count as an option there would be very helpful.

    This has been raised in pre-purchase question and this resource mentioned: http://roshanbh.com.np/2008/10/jquery-plugin-word-counter-textarea.html but having looked there I have no ideas about how to implement the code and don't have the resource to justify hiring a developer who does.

    Add my vote for the integration of a word count feature :)

    Posted 11 years ago on Wednesday June 27, 2012 | Permalink
  2. Thank you for the suggestion.

    Posted 11 years ago on Wednesday June 27, 2012 | Permalink
  3. hellotoby
    Member

    This can be done using the reverse of this post: http://www.gravityhelp.com/forums/topic/minimum-word-count-custom-validation

    I added this to my functions.php file using the WordPress theme editor. Change the name of the filter to suit your situation.

    Filter name: gform_field_validation_11_70
    11 refers to my form id
    70 refers to the field id on my form

    Here is my modified solution:

    // Added custom validation for minimum word count
    	add_filter("gform_field_validation_1_6", "validate_word_count", 10, 4);
    
    	function validate_word_count($result, $value, $form, $field){
    	    if (str_word_count($value) > 50) // Maximum number of words
    	    {
    	        $result["is_valid"] = false;
    	        $result["message"] = "Please enter 50 words or less.";
    	    }
    	    return $result;
    	}
    Posted 11 years ago on Wednesday February 13, 2013 | Permalink
  4. Another way to do it... we started with the resource in http://roshanbh.com.np/2008/10/jquery-plugin-word-counter-textarea.html but wound up a little differently.

    Save this in /themes/yourtheme/js/ as jquery.gravity_wordcount.js:

    jQuery(document).ready(function($) {
        $("[class*='count[']").each(function() {
            var elClass = $(this).attr('class');
            var maxWords = 0;
            var countControl = elClass.substring((elClass.indexOf('['))+1, elClass.lastIndexOf(']')).split(',');
    
            if (countControl.length > 1) {
                minWords = countControl[0];
                maxWords = countControl[1];
            } else {
                maxWords = countControl[0];
            }
    
            $(this).find('textarea').bind('keyup click blur focus change paste', function() {
                var numWords = jQuery.trim($(this).val()).replace(/\s+/g," ").split(' ').length;
    
                if ($(this).val() === '') {
                    numWords = 0;
                }
    
                $(this).siblings('.word-count-wrapper').children('.word-count').text(numWords);
    
                if (numWords > maxWords && maxWords != 0) {
                    $(this).siblings('.word-count-wrapper').addClass('error');
    
                    var trimmedString = '';
                    var wordArray = $(this).val().split(/[\s\.\?]+/);
                    for (var i = 0; i < maxWords; i++) {
                        trimmedString += wordArray[i] + ' ';
                    }
    
                    $(this).val(trimmedString);
                }
                else if (numWords == maxWords && maxWords != 0) {
                    $(this).siblings('.word-count-wrapper').addClass('error');
                }
                else {
                    $(this).siblings('.word-count-wrapper').removeClass('error');
                }
    
            }).after('<span class="word-count-wrapper">Total Word Count: <span class="word-count">0</span></span>');
        });
    });

    In functions.php:

    /* Gravity Forms Word Count Script */
    function els_load_scripts() {
    	wp_enqueue_script('gravity-forms-word-count', get_stylesheet_directory_uri() . '/js/jquery.gravity_word_count.js', array('jquery'), '0.1', true);
    }
    add_action('wp_enqueue_scripts', 'els_load_scripts');

    Then in the form, for fields that need the word count, add the class ‘els-word-count[300].' Change [300] as needed for the maximum words that can be added to that particular field.

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  5. This functionality is also available as a plugin here:

    GP Word Count for Gravity Forms
    http://gravitywiz.com/documentation/gp-word-count/

    Posted 9 years ago on Friday August 22, 2014 | Permalink