Limit Characters or Words in WordPress Form Fields

Follow the guide below to limit the number of characters or words you can accept in your WordPress FireBox form fields, such as text fields, textarea fields, hidden fields, dropdown fields, and radio fields.

Enable PHP Scripts

This guide requires that PHP Scripts be enabled to perform these validations. To enable PHP Scripts, go to FireBox > Settings > Advanced > enable PHP Scripts.

Limit Form Field by Characters

To limit a text form field and require up to X characters, edit your FireBox campaign, go to PHP Scripts section > Form Process, and add:

// Enter your text field name
$text_field_name = 'name';

// Enter the maximum number of characters allowed
$text_field_characters_limit = 10;

// Enter the error message to be displayed if the limit is exceeded
$text_field_error_message = 'The name field can contain up to 10 characters';

// Do not edit below
if (isset($values[$text_field_name]) && strlen($values[$text_field_name]) > $text_field_characters_limit) {
    throw new \Exception($text_field_error_message);
}

You must set up the following:

  • Set your text field name in $text_field_name
  • Set the max characters allowed in $text_field_characters_limit
  • Set the error message that will appear when the criteria are not met in $text_field_error_message

Limit Form Field by Words

To limit a text form field and require up to X words, edit your FireBox campaign, go to PHP Scripts section > Form Process, and add:

// Enter your text field name
$text_field_name = 'name';

// Enter the maximum number of words allowed
$text_field_words_limit = 3;

// Enter the error message to be displayed if the limit is exceeded
$text_field_error_message = 'The name field can contain up to 3 words';

// Do not edit below
if (isset($values[$text_field_name]) && str_word_count($values[$text_field_name]) > $text_field_words_limit) {
    throw new \Exception($text_field_error_message);
}

You must set up the following:

  • Set your text field name in $text_field_name
  • Set the max words allowed in $text_field_words_limit
  • Set the error message that will appear when the criteria are not met in $text_field_error_message
Was this helpful?