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.

December 27, 2022: Added support for validating character counts in List fields.

June 1, 2022: Fixed issue where the max character count would not be used if the minimum was zero or not set.

March 25, 2022: Migrated to Snippet Library.

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.

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.
  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'                => 123,
	'field_id'               => 4,
	'min_chars'              => 3,
	'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!

Comments

  1. Christopher Roberson
    Christopher Roberson June 4, 2024 at 1:13 pm

    Trying to limit characters for each field in a 2 column list. Use case is for a memorial brick that can have 2 lines, each with a max of 16 characters each. For the field ID, I’ve tried just the ID (16 in this case) itself, 16.1 and ‘16.1’, and none are working for me. How do I get character limitations to apply to fields within a list?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff June 5, 2024 at 5:38 am

      Hi Christopher,

      The snippet currently doesn’t support List Fields. If you have a Gravity Perks licence, you can submit this as a feature request via our support form, so our developers can have a look and see if this can be supported.

      Best,

    2. Christopher Roberson
      Christopher Roberson June 6, 2024 at 12:50 pm

      According to the update section above, on December 27, 2022 it mentions adding list field support, no?

    3. Samuel Bassah
      Samuel Bassah Staff June 7, 2024 at 6:55 am

      HI Christopher,

      You’re right. Thanks for pointing that out. Support has already been added for List fields. I just tested it using just the ID of the List field, 10, as the ‘field_id’ and it applied the limit to all the columns on the List field as seen here: https://ibb.co/GR3SY2p. Could you try this on a different setup and see how it works?

      Best,

  2. Simon Topple
    Simon Topple October 12, 2023 at 6:42 am

    This looks really useful. However I have a need for this on a name block – it has a prefix, first, middle and last name in the block but the code above only refers to a field which isn’t an array.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff October 12, 2023 at 10:32 am

      Hi Simon,

      For a multi-input field, you’ll need to add the sub-field modifier to the field ID. You’ll also need multiple configurations, one for each sub-field. So you will something like so;

      First Name

      new GW_Minimum_Characters( array( 'form_id' => 123, 'field_id' => '4.3', 'min_chars' => 3, '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.' ), ) );

      Last Name

      new GW_Minimum_Characters( array( 'form_id' => 123, 'field_id' => '4.6', 'min_chars' => 3, '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.' ), ) );

      Here’s the complete list of the Name field modifiers. https://docs.gravityforms.com/name/#h-modifiers

  3. Roy Vathorst
    Roy Vathorst July 3, 2023 at 9:40 am

    Hi,

    I would to set a max-character limit of 10. So the user instantly sees that after the input of 0123456789 nothing else will show because 10 out of the 10 input length is used. The minimum is also 10. How can I do this :)

    Reply
  4. Michelle
    Michelle February 2, 2023 at 5:48 pm

    We have a form with a name field where the client wants the last name to be limited to just the last initial. Is that possible with this plugin? Would we just update the field ID to be “1.6” or whatever the ID is of the last name field specifically, and then set the min/max characters both to 1?

    Thank you!

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff February 3, 2023 at 5:43 am

      Hi Michelle,

      Yes, you can use the snippet to set a max limit of 1 to the Last name field with a parameter value of 1.6 if the Name field ID is 1. One main concern with this solution for your use case, will be that the validation will be after the user submits the form. So if they enter more characters they will be notified after they submit the form.

    2. Michelle
      Michelle February 3, 2023 at 5:20 pm

      Yeah, it would be preferable for the field to just not let them add more than the one character to the last name field, but I think the validation after submission can also work. The client really just wants to not store the full last name, so I think as long as we have an option that can do that, they’ll be happy. Thank you!

  5. Steve
    Steve August 26, 2022 at 5:48 am

    Hi, hoping for some help. Looking to add a minimum character requirement for a list field, column. This will stop customers giving an inadequate description of an item.

    Happy to clone and do individual code for each list field, as some have 2 columns, others have 3.

    Thanks. Steve

    Reply
    1. Dario Space
      Dario Space August 26, 2022 at 10:40 am

      Hi Steve,

      This is not supported out of the box. If you have an active Gravity Perks license, you get in touch with us via our support form so we can dig into this.

      Best,

    1. Samuel Bassah
      Samuel Bassah Staff June 10, 2022 at 5:51 am

      Hi Juan,

      If I understand correctly you want to set the maximum range of a number field. If so, then you can use the GF range feature on the Number field.

      Best,

  6. Karie
    Karie May 24, 2022 at 7:51 pm

    I have a form that a customer needs to either input something in a text field that is 5 characters or 9 characters. Is there a way that I can use this code to allow the customer to enter 5 or 9 characters but any other character amount would trigger the error message?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff May 25, 2022 at 6:08 am

      Hi Karie,

      This will require some customization to the snippet to get it working the way you want. If you have an active Pro Gravity Perks License, you can get in touch via our support form so we can forward your request to our developers to assist you.

      Best,

  7. test
    test March 19, 2022 at 3:45 pm

    Hello,

    I would like to add a character limit for the name field in gravity forms. I put your code in the functions.php but it does not work and the below error occurred.

    Uncaught TypeError: strlen(): Argument #1 ($str) must be of type string

    What should I do?

    Reply
    1. test
      test March 19, 2022 at 4:30 pm

      Now, I have corrected the code for name field using 2.3 id for the field id and the error was resolved.

      But unfortunately it does not work and when I enter the value greater than character limit in the field, no error appears.

      what should I do?

    2. Samuel Bassah
      Samuel Bassah Staff March 21, 2022 at 5:56 am

      Hi,

      It appears the snippet doesn’t work on Name fields. I will pass this over to our developers to take a look into this and support for Name fields. We’ll reply to this comment with an update when this has been resolved. If you’re also a Gravity Customer, you can get in touch with us via our support form to follow up on this.

      Best,

    3. test
      test March 21, 2022 at 5:34 pm

      Hello Samuel,

      Thank you so much! It is a necessary feature to restrict Name field! I’m waiting for this feature to work, please let me know when it works.

      Thank you in advanced

    4. Dario Space
      Dario Space April 8, 2022 at 4:54 pm

      Hi,

      You should target the Name with field ID with their modifiers: .3 for the name and .6 for the last name.

      I hope this helps!

      Best,

    5. test
      test April 9, 2022 at 5:05 pm

      Hello Dario,

      Thank you for your response!

      Yes, I put the Name with field ID with their modifiers, here is my code. What is my mistake?

      new GW_Minimum_Characters( array( ‘form_id’ => 3, ‘field_id’ => 2.3, ‘min_chars’ => 2, ‘max_chars’ => 60, ‘min_validation_message’ => __( ‘Oops! You need to enter at least %s characters.’ ), ‘max_validation_message’ => __( ‘Oops! You can only enter %s characters.’ ) ) ); new GW_Minimum_Characters( array( ‘form_id’ => 3, ‘field_id’ => 2.6, ‘min_chars’ => 2, ‘max_chars’ => 60, ‘min_validation_message’ => __( ‘Oops! You need to enter at least %s characters.’ ), ‘max_validation_message’ => __( ‘Oops! You can only enter %s characters.’ ) ) );

    6. test
      test April 9, 2022 at 5:06 pm

      new GW_Minimum_Characters( array( ‘form_id’ => 3, ‘field_id’ => 2.3, ‘min_chars’ => 2, ‘max_chars’ => 60, ‘min_validation_message’ => __( ‘Oops! You need to enter at least %s characters.’ ), ‘max_validation_message’ => __( ‘Oops! You can only enter %s characters.’ ) ) );

      new GW_Minimum_Characters( array( ‘form_id’ => 3, ‘field_id’ => 2.6, ‘min_chars’ => 2, ‘max_chars’ => 60, ‘min_validation_message’ => __( ‘Oops! You need to enter at least %s characters.’ ), ‘max_validation_message’ => __( ‘Oops! You can only enter %s characters.’ ) ) );

    7. Dario Space
      Dario Space April 10, 2022 at 3:47 pm

      Hi,

      I can’t confirm, it seems to be working as expected when testing. We’ll be happy to investigate this if you get in touch with us via our Support Form.

      Best,

    1. John Baker
      John Baker 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.

    1. David Smith
      David Smith Staff 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.

  8. Tayler
    Tayler 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
    1. David Smith
      David Smith Staff June 13, 2018 at 3:59 am

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

  9. Mellisa
    Mellisa 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
    1. David Smith
      David Smith Staff 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.

  10. Sam Mitzmann
    Sam Mitzmann 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
  11. Cray
    Cray 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
    1. David Smith
      David Smith Staff 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

  12. Salvatore
    Salvatore 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
  13. hm
    hm 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
    1. hm
      hm 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?

    2. Need Help
      Need Help 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.

  14. Lori
    Lori 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
  15. Joern
    Joern 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
    1. David Smith
      David Smith Staff 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.' ) ) );

  16. Mason L
    Mason L 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
  17. Greg
    Greg 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
    1. David Smith
      David Smith Staff 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
    Stan 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
    1. David Smith
      David Smith Staff 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.

Leave a Reply

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

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Require Minimum/Maximum Character Limit for Gravity Forms

This field is for validation purposes and should be left unchanged.