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?
- 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
- Copy and paste the snippet into your theme’s functions.php file.
- 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 themax_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 46+ essential add-ons for Gravity Forms with support you can count on.
Perfect for my need ! Thanks
You’re welcome.
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!
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.
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!
Hi Michelle,
You’re welcome. Glad it will work for you.
Best,
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
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,
Hi Steve,
This is now supported in version 1.3 of the snippet.
Thanks for writing in!
Hello, how can I make the maximum that I set to be numeric not letters?
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,
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?
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,