When creating an HTML form, it’s possible to add a maxlength attribute to any input box. Since WPForms generates all field HTML for you, you can instead add the maxlength attribute using jQuery.
We’ll load this jQuery within PHP so that it can be added right into functions.php or wherever you choose to keep custom PHP on your site.
Note: For shorter character limits, or other format requirements, please consider using a custom input mask instead of the code below.
This code will apply a maximum length of 40 characters to any input field with a CSS class named
wpf-char-limit. When changing this limit, be sure to do so in two places.
To add this class to a field, you’ll need to open the form builder, click the field to open its Field Options panel, and open the Advanced Options section.
Since this code will simply enforce a character limit, it’s also a good idea to let your users know about this limit within the field description.