• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Custom php code for validation to exclude words?

Custom php code for validation to exclude words? 9 years 9 months ago #28254

Need to customize validation to exclude a set of words. Need help with how this coding should be done. Thanks!
The administrator has disabled public write access.

Custom php code for validation to exclude words? 9 years 9 months ago #28276

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
You will need a PHP script to check if the field value contains a banned word and if so, invalidate the field
function checkstring($string,$banned_words) {
    foreach($banned_words as $banned_word) {
        if(stristr($string,$banned_word)){
            return false;
        }
    }
    return true;
}
 
$string = preg_replace('/\s+/','', $_POST['form']['field_name']);
$banned_words = array('whatever','bad','word','you','want','goes','here');
 
if (!checkstring($string,$banned_words)) 
  $invalid[] = RSFormProHelper::getComponentId("field_name");

http://www.rsjoomla.com/support/documentation/view-article/602-php-scripts.html

PS:Keep in mind this is an example and not a solution
My help is not official customer support. To receive your support, submit a ticket by clicking here
Last Edit: 9 years 9 months ago by cosmin.cristea.
The administrator has disabled public write access.

Custom php code for validation to exclude words? 9 years 5 months ago #29304

  • gverdone
  • gverdone's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 2
Hi,

The code works good, but i want to check a array also for word combinations like;

array('whatever bad word','you want goes',' here');


Can some one help please?

Thanks
The administrator has disabled public write access.

Custom php code for validation to exclude words? 9 years 5 months ago #29311

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
The script checks for a "bad" word inside your string. The position of the word in the string is irrelevant.
My help is not official customer support. To receive your support, submit a ticket by clicking here
The administrator has disabled public write access.

Custom php code for validation to exclude words? 9 years 5 months ago #29315

  • gverdone
  • gverdone's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 2
Yes i know, but i want to check not only one word a time but word combinations like 'word combination'
because whit the current code its not possible its only checks single words like 'word','combination'

I found out that You have to use preg_replace with regex
regex removes the whitespaces from the input string
so when you use 'word combination', then it will be considered as 'wordcombination'!

I replaced this line:
$string = preg_replace('/\s+/','', $_POST['form']['field_name']);
Replaced with:
$string = trim($_POST['form']['field_name']);

This worked for me!
Last Edit: 9 years 5 months ago by gverdone.
The administrator has disabled public write access.

Custom php code for validation to exclude words? 7 years 3 months ago #36322

  • stan9
  • stan9's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 5
The code provided above helped me a lot. I'm providing the code I ended up with, along with more detailed instructions that I commented out. I'm not a programmer, so it took me a while to figure it out. Maybe this will help someone else.

===========================================================

// PHP SCRIPT TO BLOCK SUBMISSION OF SPAM/BAD WORDS
// PLACE IN FORM PROPERTIES, PHP SCRIPTS SECTION, SCRIPT CALLED ON FORM PROCESS

function checkstring($string,$banned_words) {
foreach($banned_words as $banned_word) {
if(stristr($string,$banned_word)){
return false;
}
}
return true;
}

// CHANGE Message_Field_Name TO YOUR FORM'S MESSAGE FIELD NAME
$string = preg_replace('/\s+/','', $_POST);

// CHANGE/ADD WORDS YOU WOULD LIKE TRIGGER REJECTING THE SUBMISSION
$banned_words = array('viagra','erectile','http://','https://','xanax','valium','celebrex','xxx','pharmacy','sex','mortgage','insurance','oprah','bankrupt','creditor','casino','finance','babes');

if (!checkstring($string,$banned_words))
// CHANGE Message_Field_Name TO THE FIELD NAME IN YOUR FORM WHERE AN ERROR WILL BE INDICATED
// THE ERROR MESSAGE WILL APPEAR IN THIS FIELD
$invalid[] = RSFormProHelper::getComponentId("Message_Field_Name");
Last Edit: 7 years 3 months ago by stan9. Reason: I removed errors.
The administrator has disabled public write access.

Custom php code for validation to exclude words? 3 years 8 months ago #40475

  • duncan
  • duncan's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 19
  • Thank you received: 1
Hi,

I am trying to ban some words from submission. I have a field called 'Question' and my form name is 'Page_Contact_form' and I want to ban the word 'Amoxil'

I am using the original script posted at the top of this post and it is not working and the form is still being sent. I can't see what's wrong, can anybody help. Here is my code:
function checkstring($string,$banned_words) {
    foreach($banned_words as $banned_word) {
        if(stristr($string,$banned_word)){
            return false;
        }
    }
    return true;
}
 
$string = preg_replace('/\s+/','', $_POST['Page_Contact_form']['Question']);
$banned_words = array('Amoxil');
 
if (!checkstring($string,$banned_words)) 
  $invalid[] = RSFormProHelper::getComponentId("Question");

Thanking you in advance.

D
Last Edit: 3 years 8 months ago by duncan.
The administrator has disabled public write access.

Custom php code for validation to exclude words? 2 years 7 months ago #41503

  • modernmagic
  • modernmagic's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 32
  • Thank you received: 2
Disregard. I figured it out.
Last Edit: 2 years 7 months ago by modernmagic.
The administrator has disabled public write access.

Custom php code for validation to exclude words? 2 years 2 months ago #41815

We get a lot of HTML in our spam, especially those with links. Any thoughts on how to block messages that contain:
<a href=

Using this format in the banned_words variable did not work:
$banned_words = array('<a href=');
The administrator has disabled public write access.

Custom php code for validation to exclude words? 2 years 2 months ago #41816

Any thoughts on filtering by html link? This code is not working for html links:
function checkstring($string,$banned_words) {
 
	foreach($banned_words as $banned_word) {
 
		if(stristr($string,$banned_word)){
 
			return false;
		}
	}
 
	return true;
 
}
 
$string = preg_replace('/\s+/','', $_POST);
 
$banned_words = array('&lt;a href=');
 
if (!checkstring($string,$banned_words))
$invalid[] = RSFormProHelper::getComponentId("Message");
The administrator has disabled public write access.

Custom php code for validation to exclude words? 2 years 2 months ago #41820

Here is the working code:
// Prevent submissions containing banned strings like html links
function checkstring($string,$banned_words) {
 
	foreach($banned_words as $banned_word) {
 
		if(stristr($string,$banned_word)){
 
			return false;
		}
	}
 
	return true;
 
}
 
$string = trim($_POST['form']['Message']);
 
$banned_words = array('<a href=');
 
if (!checkstring($string,$banned_words))
$invalid[] = RSFormProHelper::getComponentId("Message");
// END Banned Strings
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!