No worries, it’s easy to miss if you’ve never used a block based editor like this before, and the fact that I don’t have a tutorial or video doesn’t help either!
HDForms does not have a native way to set character limits, but it IS absolutely possible, as long as you are willing to add a small code snippet to your theme’s functions.php
. If you are unable or unwilling to do this, then that’s OK, you can ignore everything below.
– – – – – – – – – – – – – – – – – – –
I built in “actions” to HDForms that allows us to run custom functions at different times in the form process such as on form initialize, before form submits, and after form submits.
In our case, we can use the on init action to set a maxlength
of your textarea field.
The function you will need to add to your theme’s functions.php
file is. The below will look complicated, but that’s really only because I will be going into depth explaining how everything works ??
function hdf_custom_textarea_limit($formid)
{
?>
<script>
function hdf_custom_textarea_limit()
{
const maxlength = 10; // set the maximum character limit
const fieldID = "hdf_textarea_XXXXXX"; // replace with the field ID of the textarea
const el = document.getElementById(fieldID);
el.setAttribute("maxlength", maxlength);
}
</script>
<?php
}
add_action("hdf_after", "hdf_custom_textarea_limit", 10, 1);
REMEMBER TO MAKE A BACKUP of the file in case you make a mistake copy/pasting.
In that function, there are two variables that you will need to edit.
maxlength
is currently set to 10
, but you can change it to any number you need.
fieldID
is the ID of your textarea field. This can be found by editing the form and clicking on the textarea field. In the sidebar, you will see something similar to “hdf_textarea_xxxxxx”. This is the ID that you need. We can also apply the change to ALL textareas if that’s what you prefer – just let me know.
And finally, you need to tell the form to run that custom function when the form has initialized. To do this, edit the form, and under Form Settings, select “extra +” at the bottom of the sidebar. Enter hdf_custom_textarea_limit
in the “Form init” field.