Gravity Wiz

Magically enhanced tutorials, snippets and plugins for Gravity Forms!

  • Gravity Perks
    • Gravity Perks
    • Tutorials & Snippets
    • About
  • Support
    • Documentation
    • Support
    • Account

Require Minimum/Maximum Character Limit for Gravity Forms

Adds support for requiring a minimum and maximum number of characters for text-based Gravity Form fields.

Last updated September 23, 2019 | Written by Jordan Smith 41 Comments

  • May 30, 2014: Added support for requiring a maximum character count as well.

Have you ever received a generic comment or question in a form submission and wondered if there was a way to discourage this type of response? We’ve whipped up a little snippet that you can use to encourage a more descriptive comment from users submitting your forms.

View DemoShow CodeDownload Code
<?php
/**
* Gravity Wiz // Require Minimum Character Limit for Gravity Forms
*
* Adds support for requiring a minimum number of characters for text-based Gravity Form fields.
*
* @version 1.0
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2013 Gravity Wiz
*/
class GW_Minimum_Characters {
public function __construct( $args = array() ) {
// make sure we're running the required minimum version of Gravity Forms
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.7', '>=' ) )
return;
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_id' => false,
'min_chars' => 0,
'max_chars' => false,
'validation_message' => false,
'min_validation_message' => __( 'Please enter at least %s characters.' ),
'max_validation_message' => __( 'You may only enter %s characters.' )
) );
extract( $this->_args );
if( ! $form_id || ! $field_id || ! $min_chars )
return;
// time for hooks
add_filter( "gform_field_validation_{$form_id}_{$field_id}", array( $this, 'validate_character_count' ), 10, 4 );
}
public function validate_character_count( $result, $value, $form, $field ) {
$char_count = strlen( $value );
$is_min_reached = $this->_args['min_chars'] !== false && $char_count >= $this->_args['min_chars'];
$is_max_exceeded = $this->_args['max_chars'] !== false && $char_count > $this->_args['max_chars'];
if( ! $is_min_reached ) {
$message = $this->_args['validation_message'];
if( ! $message )
$message = $this->_args['min_validation_message'];
$result['is_valid'] = false;
$result['message'] = sprintf( $message, $this->_args['min_chars'] );
} else if( $is_max_exceeded ) {
$message = $this->_args['max_validation_message'];
if( ! $message )
$message = $this->_args['validation_message'];
$result['is_valid'] = false;
$result['message'] = sprintf( $message, $this->_args['max_chars'] );
}
return $result;
}
}
# Configuration
new GW_Minimum_Characters( array(
'form_id' => 524,
'field_id' => 1,
'min_chars' => 4,
'max_chars' => 5,
'min_validation_message' => __( 'Oops! You need to enter at least %s characters.' ),
'max_validation_message' => __( 'Oops! You can only enter %s characters.' )
) );
view raw gw-gravity-forms-minimum-characters.php hosted with ❤ by GitHub

How do I get started?

  1. This will work with even Gravity Forms v1.0 but that’s no excuse not to be running the latest version.
    • Already have a license? Download Latest Gravity Forms
    • Need a license? Buy Gravity Forms
  2. Copy and paste the snippet into your theme’s functions.php file.
  3. Once you have installed the snippet, you just need to configure the snippet for your Gravity form. See the usage instructions below.

Usage

new GW_Minimum_Characters( array(
'form_id' => 524,
'field_id' => 1,
'min_chars' => 4,
'max_chars' => 5,
'min_validation_message' => __( 'Oops! You need to enter at least %s characters.' ),
'max_validation_message' => __( 'Oops! You can only enter %s characters.' )
) );

Parameters

The form_id, field_id, and min_chars parameters are required. Incorrect values will cause the snippet to fail silently; no error message will be displayed.

  • form_id (integer) (required) The form ID of your form.
  • field_id (integer) (required) The field ID of the field that you would like to enable a minimum character limit.
  • min_chars (integer) (required) The minimum character limit that you would like to apply to the field.
  • validation_message (string) (optional) The validation message that you would like to display to the user when the minimum character limit is not reached.
  • min_validation_message (string) (optional) The validation message that you would like to display to the user when the minimum character limit is not reached. Added to better differentiate between the validation_message and the max_validation_message parameters.
  • max_validation_message (string) (optional) The validation message that you would like to display to the user when the maxiumum character limit is exceeded.

Can I add this to more than one field per form?

You sure can! You can add this to as many fields on as many forms as you want. Simply copy and paste the configuration code listed above and update the values for your new form and field.

If you use it and like it, let us know. We’d love to hear the different reasons how you found this code useful!

Did this resource help you do something awesome with Gravity Forms? Then you'll absolutely love Gravity Perks; a suite of 32+ essential add-ons for Gravity Forms with support you can count on.

  • View All Perks
  • Buy Gravity Perks

Filed Under: Snippets requirements

Comments

  1. Austin says

    April 29, 2019 at 12:52 pm

    Hi David Thanks for this code. can we use character limit for gravity view too?

    Reply
    • David Smith says

      April 29, 2019 at 1:00 pm

      Hm, not sure. Did you try? Let us know the result. ?

  2. John Baker says

    April 24, 2019 at 2:44 am

    Hi David! can i use this snippet for search filed?

    Reply
    • David Smith says

      April 24, 2019 at 6:27 am

      What is the search field?

    • John Baker says

      April 24, 2019 at 2:11 pm

      i have search field and i want user cant enter less than 10 character and entry first hidden from user. i use gravity view can i fix it? Thanks.

  3. Clifford Paulick says

    October 26, 2018 at 11:23 pm

    Well I needed to adapt this for my own use to support array-like fields (e.g. Address and Name)

    So here you go, everybody: https://github.com/cliffordp/gf-gw-req-char-length

    Gravity Wiz folks, feel free to co-own it with me, link it, etc. as you wish.

    Reply
    • David Smith says

      November 8, 2018 at 3:24 pm

      Thanks for sharing, Clifford!

  4. Jonathan says

    August 29, 2018 at 2:13 pm

    Can the code be modified to count the characters in a specific field and the post the number to another form field?

    Reply
    • David Smith says

      August 31, 2018 at 7:30 am

      This snippet could be used as the base for the PHP validation of that functionality; however, you would need to write a bit of JS to handle counting the characters and populating that count into another field on the frontend.

    • Thomas says

      January 4, 2019 at 12:56 pm

      Hello Jonathan,

      do you have a solution for the counting.

      I have the same problem.

      Thanks, Thomas

  5. Tayler says

    June 12, 2018 at 1:18 pm

    Is it possible to use this for the address fields? They are broken down into sub-ID’s and I haven’t been able to make it work for individual fields. When I apply it the whole address section (with the simple ID) it doesn’t seem to recognize that there are any characters anywhere. Am I doing it wrong, or will this just not work for the address field?

    Reply
    • David Smith says

      June 13, 2018 at 3:59 am

      Hi Taylor, this script doesn’t support checking for input-specific character counts.

  6. Mellisa says

    January 23, 2018 at 4:31 am

    Hi

    This was asked before but was not answered. Your code works great, however it only works based on form_id and field_id. I need this to go a step further, I need it to work on a cell id as well.

    I want to be able to tell it which form id, which field id and which cell in that field id to apply the max/min rule.

    Please help.

    Reply
    • David Smith says

      January 23, 2018 at 7:24 am

      Hi Mellisa, we do not have plans to add support for List fields for this snippet. You might consider hiring a developer to add this enhancement via a service like Codeable.io.

  7. Sam Mitzmann says

    December 18, 2017 at 4:03 pm

    I am getting this error when trying to activate the snippet:

    The code snippet you are trying to save produced a fatal error on line 12:

    Cannot redeclare class GW_Minimum_Characters

    Reply
  8. Cray says

    September 1, 2017 at 11:27 am

    Hi,

    is it possible to limit the characters in a special field for every form? Maybe with a parameter name for the input field?! We have dozens of forms and couldn’t built a function for all of them ;)

    Reply
    • David Smith says

      September 6, 2017 at 8:30 am

      Hi Cray, this is certainly possible but would required customizing the snippet. Feel free to reach out to me for a quote: david at gravitywiz dot com

  9. Salvatore says

    July 5, 2017 at 3:41 am

    Hi,

    Iam not really understanding in which File I have to copy this that I get minimum Characters?

    Please provide me her.

    Many THX

    Reply
    • David Smith says

      July 5, 2017 at 10:55 am

      Hi Salvatore, this resource might help: https://gravitywiz.com/documentation/snippet-troubleshooting/

  10. Sam Mitzmann says

    February 2, 2017 at 6:51 pm

    Hey David, I am getting a fatal error from this snippet. It looks like its coming from the validation messages.

    Reply
  11. Jonathan says

    September 28, 2016 at 5:43 pm

    Thanks, very helpful.

    Reply
    • David Smith says

      September 28, 2016 at 6:44 pm

      Glad to help. :)

  12. hm says

    May 4, 2015 at 12:25 pm

    Hi, is this possible to use on Gravity Forms list items, where each item should be limited. I attempted this using the list ID and it didnt work.

    Reply
    • David Smith says

      May 4, 2015 at 2:04 pm

      Not sure I understand your question?

    • hm says

      May 4, 2015 at 3:37 pm

      I would like to constrain text input on list items in Gravity Forms. Where each item in the list is constrained to x amount of characters. When I attempt to use the plugin as you have shown, with the Id of the list and form id, it always returns an error. I assume this has to with the fact list inputs are dynamically created or are a set. Is it possible to set some parameters that would fix that?

    • Ed says

      July 20, 2015 at 2:24 pm

      try something of the sort:

      http://pastie.org/private/o3gmr8ndxrs8exx6br2sjw

    • Need Help says

      August 23, 2015 at 11:39 pm

      have you implemented this correctly? do you mind sharing the code if yes.

      this is my problem too, i dont get how to implement this on list (Advanced Fields) .. :(

      thanks.

  13. Lori says

    November 17, 2014 at 4:19 pm

    I would to see something set up like this but instead of min & max characters, I would like to ban certain words such as a specific course name which requires a user to complete a paper application and not complete the online form.

    Reply
    • David Smith says

      November 17, 2014 at 5:26 pm

      Hi Lori, this is possible with the GP Comment Blacklist perk (available with Gravity Perks).

  14. Joern says

    November 17, 2014 at 3:45 pm

    Could you please give an example, how to add the configuration values for more than one field ? I tried several ways but none seemed to work:

    new GW_Minimum_Characters( array( ‘form_id’ => 2, ‘field_id’ => 12, ‘min_chars’ => 200, ‘form_id’ => 2, ‘field_id’ => 3, ‘min_chars’ => 40, ‘min_validation_message’ => __( ‘Oops! You need to enter at least %s characters.’ ) ) );

    Reply
    • David Smith says

      November 17, 2014 at 5:31 pm

      Hi Joern, you would create two “instances” of the GW_Minimum_Characters class. Here’s an example:

      new GW_Minimum_Characters( array( 'form_id' => 2, 'field_id' => 12, 'min_chars' => 200, 'min_validation_message' => __( 'Oops! You need to enter at least %s characters.' ) ) );

      new GW_Minimum_Characters( array( 'form_id' => 2, 'field_id' => 3, 'min_chars' => 40, 'min_validation_message' => __( 'Oops! You need to enter at least %s characters.' ) ) );

  15. Mason L says

    October 17, 2014 at 4:06 am

    Hey David. I have the snippet exact, other than field id and form id.

    Reply
  16. Mason L says

    October 8, 2014 at 6:47 pm

    Hey David, When I put in the code you’ve provided, I get a fatal error:

    Fatal error: Class ‘GW_Minimum_Characters’ not found in /home/content/81/9120881/html/clasificadosoutdoordotcom/wp-content/themes/ClasificadosOnline/functions.php on line 34

    This is line 34: “new GW_Minimum_Characters( array(” Should I change that to something else? Sorry I’m not the best w/ php.

    Reply
    • Gisele Grenier says

      October 14, 2014 at 11:23 am

      I’m getting a similar error: Fatal error: Class ‘GW_Minimum_Characters’ not found…

    • David Smith says

      October 17, 2014 at 1:13 am

      Hi Gisele, are you including the entire snippet as well as the configuration code?

  17. Greg says

    September 10, 2014 at 7:21 pm

    Do you think there would be a way to make it where a field would have to be exactly a certain length. I know you could set ‘min_chars’ => 8, ‘max_chars’ => 8 and that would essentiall do what is needed. But what if you wanted the submission to be either 8 or 11 characters long. Do you think this Is this something that is possible?

    Not really looking for the code to be written for me, but I am very new to gravity forms and php in general, and do not want to waste hours trying to figure it out if that gravity forms simply cannot handle that functionality.

    Thanks for this tutorial. It is really insightful.

    Reply
    • David Smith says

      September 10, 2014 at 10:37 pm

      Hi Greg, this might be possible with this current snippet by instantiating the class twice with two different lengths. Example: http://pastie.org/9543690

      If this doesn’t work, it would still be possible with some customization to the code (a.k.a Gravity Forms is capable of handling this with a small amount of modification).

  18. Stan says

    May 30, 2014 at 7:41 am

    Is there a similar function for limiting the max number of characters to be entered? I have tried ‘max_chars’ but doent seem to take?

    Reply
    • Stan says

      May 30, 2014 at 7:43 am

      We are hoping to control this via code and via the character limit on the front-end…

    • David Smith says

      May 30, 2014 at 6:14 pm

      Hi Stan, I’ve updated the snippet above to now support setting a maximum number of characters. You can specify a “max_chars” and a “max_validation_message” once you’ve updated your copy of the snippet with the updated copy above.

      I’d love to make this a perk (which would include a UI for managing this per field). If you’re interested in commissioning the development, get in touch.

Trackbacks

  1. Gravity Forms: Require minimum/maximum characters in input field says:
    September 22, 2017 at 2:34 pm

    […] I found this solution: (full code here: https://gravitywiz.com/require-minimum-character-limit-gravity-forms/) […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

  • How To (64)
  • News (21)
  • Plugins (14)
  • Releases (7)
  • Resource (3)
  • Snippets (58)
  • Tutorials (57)
  • Updates (104)

Recent Posts

  • How to Send a Follow-Up and Pre-Fill Information
  • How to Update Posts with Gravity Forms
  • Gravity Wiz Weekly #104
  • The Complete Guide to Using Gravity Forms With Zapier
  • Gravity Wiz Weekly #103

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2021 · Powered by WordPress · Gravity Wiz LLC

  • Support
  • Affiliates
  • About
  • Sitemap
  • Gravity Perks
    ▼
    • Gravity Perks
    • Tutorials & Snippets
    • About
  • Support
    ▼
    • Documentation
    • Support
    • Account