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?
- 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!
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?
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,
According to the update section above, on December 27, 2022 it mentions adding list field support, no?
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,
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.
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
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 :)
Hi Roy, we’ve already followed up via email. This should be possible with GP Word Count.
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,
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?
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?
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,
Hi,
Here is an updated version of the snippet that supports multi-input fields. https://gravitywiz.com/require-minimum-character-limit-gravity-forms/
Best,
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
https://gravitywiz.com/require-minimum-character-limit-gravity-forms/
Hello Samuel,
Above link is the same topic as “Require Minimum/Maximum Character Limit for Gravity Forms”.
Hi Samuel,
When the snippet works on Name fields?
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,
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.’ ) ) );
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.’ ) ) );
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,
Hi,
Ok, thank you, how can I create account in support form?
Hi,
To create an account, you’ll have to purchase a Gravity Perks license.
Best,
Hi David Thanks for this code. can we use character limit for gravity view too?
Hm, not sure. Did you try? Let us know the result. ?
Hi David! can i use this snippet for search filed?
What is the search field?
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.
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.
Thanks for sharing, Clifford!
Can the code be modified to count the characters in a specific field and the post the number to another form field?
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.
Hello Jonathan,
do you have a solution for the counting.
I have the same problem.
Thanks, Thomas
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?
Hi Taylor, this script doesn’t support checking for input-specific character counts.
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.
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.
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
[…] I found this solution: (full code here: https://gravitywiz.com/require-minimum-character-limit-gravity-forms/) […]
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 ;)
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
Hi,
Iam not really understanding in which File I have to copy this that I get minimum Characters?
Please provide me her.
Many THX
Hi Salvatore, this resource might help: https://gravitywiz.com/documentation/snippet-troubleshooting/
Hey David, I am getting a fatal error from this snippet. It looks like its coming from the validation messages.
Thanks, very helpful.
Glad to help. :)
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.
Not sure I understand your question?
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?
try something of the sort:
http://pastie.org/private/o3gmr8ndxrs8exx6br2sjw
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.
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.
Hi Lori, this is possible with the GP Comment Blacklist perk (available with Gravity Perks).
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.’ ) ) );
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.' ) ) );
Hey David. I have the snippet exact, other than field id and form id.
Hey David, When I put in the code you’ve provided, I get a fatal error:
This is line 34: “new GW_Minimum_Characters( array(” Should I change that to something else? Sorry I’m not the best w/ php.
I’m getting a similar error: Fatal error: Class ‘GW_Minimum_Characters’ not found…
Hi Gisele, are you including the entire snippet as well as the configuration code?
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.
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).
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?
We are hoping to control this via code and via the character limit on the front-end…
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.