Tassos Marinos Developer of Joomla Extensions

How to Block Form Submissions Containing Profanity (Bad Words)

Published in Convert Forms
Updated 09 Dec, 2022
Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

Would you like to block specific words from being submitted into your form? This is quite useful if you have a list of bad words you would like to filter out. The following PHP snippet will provide a helpful message to your users when they submit the form and any of your listed words appears in their submission.

Setup

To detect bad words and notify the user with a helpful message, copy the code shown below and place it into the PHP Scripts -> Form Process area of your form.

// The list of not allowed words
$not_allowed_words = [
	'dog',
	'cat',
	'elephant'
];

// The Field Name where search will be performed against the not allowed words
$field_name = 'text';

// The helpful message that will appear if bad words are found
$error_message = 'Your text contains words that are not allowed.';

// Do not edit below
foreach($not_allowed_words as $word)
{
	if (stripos($post[$field_name], $word) !== false)
	{
		throw new Exception($error_message);
	}
}

What do I need to edit in the above code?

Remember to update following parts:

  • The $not_allowed_words array with the words you would like to prevent from being submitted.
  • The $field_name with your input's Field Name.
  • The $error_message with the error message you'd like to display to your users when they enter bad words.