# Word Limits for Gravity Forms Paragraph Fields

> Lets you require a minimum and maximum number of words in Gravity Forms paragraph fields, for essays, applications and testimonials.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: gravity-forms, validation, textarea
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/gf-word-limits/

## Code

```php
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
    if ( 'textarea' !== $field->type || ! $result['is_valid'] || ! preg_match( '/\bwords-(\d+)-(\d+)\b/', (string) $field->cssClass, $m ) ) {
        return $result;
    }

    $text  = trim( wp_strip_all_tags( (string) $value ) );
    if ( '' === $text && ! $field->isRequired ) {
        return $result;
    }

    $words = preg_match_all( '/[\p{L}\p{N}\']+/u', $text );
    $min   = (int) $m[1];
    $max   = (int) $m[2];

    if ( $words < $min ) {
        $result = array( 'is_valid' => false, 'message' => sprintf( 'Please write at least %1$d words (currently %2$d).', $min, $words ) );
    } elseif ( $max > 0 && $words > $max ) {
        $result = array( 'is_valid' => false, 'message' => sprintf( 'Please keep it under %1$d words (currently %2$d).', $max, $words ) );
    }

    return $result;
}, 10, 4 );
```

## Notes

Add a CSS class such as words-50-300 to a paragraph field to require 50 to 300 words; use words-50-0 for a minimum only.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Word Limits for Gravity Forms Paragraph Fields" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
