Forums

Simple php code for banned words in textarea

shd 03 May, 2012
Hi,

I have a question. I am looking for a simple banned words php code.
I want to banned some words in my textarea.
It must be an On Submit code - before sending email.
If somebody filled in a banned word a blanc page must open with some text in it.

:mrgreen: :mrgreen: :mrgreen:
Stephanie
GreyHead 05 May, 2012
Hi Stephanie,

Here's version 1 which isn't quite what you want. It's a word censor. Add a Custom Code action to the On Submit event with this code in it:
<?php
$ban_array = array(
  'xxx' => '***',
  'coach => '*##*',
  'gucci' => '*!!*',
  'thomas sabo' => ''
);
$input_array = array(
  'input_textarea_0'
);
foreach ( $input_array as $in ) {
  foreach ( $ban_array as $k => $v ) {
    $form->data[$in] = str_ireplace($k, $v, $form->data[$in]);
  }
}
?>
Edit the $ban_array to include the list of banned words and replacements for them and add the names of the inputs you want to check to the $input_array.

The result is that this entry in the text area:
Vivamus tempor eros sit amet nulla xxx egestas. Fusce odio ante, gucci sed coach xxx, interdum sed leo. Aenean sed lacus quam, convallis porttitor massa. Nullam congue nunc massa? Nam urna mi, adipiscing et rhoncus gucci, consectetur in leo. Vestibulum ac coach eu justo interdum pulvinar thomas sabo sed xxx. 
becomes this in the email
Vivamus tempor eros sit amet nulla *** egestas. Fusce odio ante, *!!* sed *##* ***, interdum sed leo. Aenean sed lacus quam, convallis porttitor massa. Nullam congue nunc massa? Nam urna mi, adipiscing et rhoncus *!!*, consectetur in leo. Vestibulum ac *##* eu justo interdum pulvinar sed ***. 

Bob
GreyHead 05 May, 2012
Hi Stephanie,

Here's version 2 which is a Custom Serverside validation
// The code here had a bug, please see the corrected version later in this thread.

You can then use the On Fail event of the Validation action to re-display the form or to show another page.

Bob
shd 07 May, 2012
:D 😀 😀 😀
tnxxx
I will try it!
newstuff 17 Jul, 2012
I'm a bit of a noob with V4 and CSSV, but have tried a multitude of ways to do this, and however I do it, the form submits and won't check the custom server side validation

The validation is not working no matter what - it sends the email regardless. Based on the "banned words" specified, I do not want an email sent, and instead just display a web page with a message.

I am probably missing something obvious, but I just can't see it!

- here's how I have set it up:

1. The OnSubmit Event has a Custom Server Side Validation event with the following code - which checks just one input field for "banned words":

<?php
    $ban_array = array('seo','search engine','experts');
    $isvalid = true;
      if (strpos ($form->data['inputmsgtext'], $ban_array) !== false) {
        $form->validation_errors['inputmsgtext'] = 'Banned words in use!!';
        $isvalid = false;
      }
    return $isvalid;
    ?>


2. The OnFail / Event Loop has bee added to the CSSV.
GreyHead 17 Jul, 2012
Hi newstuff,

I think that the problem is that strpos doesn't take an array as the second parameter. Please try this version (change the input name to match yours):
<?php
$ban_array = array(
  'seo',
  'search engine',
  'experts'
);
$isvalid = true;
foreach ( $ban_array as $v ) {
  if ( strpos($form->data['input_textarea_1'], $v) !== false ) {
    $form->validation_errors['input_textarea_1'] = 'Banned words in use!!';
    return false;
  }
}
?>
Note that this stops at the first error - there's no point in checking more here.

There's a demo form here

Bob
newstuff 17 Jul, 2012
Bob,

Pure. Genius. Thankyou so much for replying so quickly - you're a legend.

It worked perfectly.

I thought of another use - what if I just wanted it to display a message on a new page, instead of having the errors display on the form - I'll see if I can find some examples on the forums!

Thankyou so much again.
GreyHead 17 Jul, 2012
Hi newstuff,

I extended this thread into a FAQ - there are some suggestions there for redirection.

Bob
newstuff 17 Jul, 2012
Bob,

You are an absolute legend. Thank you so much for your help (again) - you're just so great with helping everyone out here on the board - and very quick to respond as well. I am using the V4 more now, so hopefully I will get better at it as time goes on.
This topic is locked and no more replies can be posted.