October 3, 2015

Validate gravity form from unwanted text

Validate gravity form for unwanted words use the below code and put it at your functions.php

function bld_strpos_arr($haystack, $needle) {
if(!is_array($needle))
{
$needle = array($needle);
}
foreach($needle as $what) {
$pos = stripos($haystack, $what);
if( $pos !== false)
{
return true;
}
}
return false;
}

/*
* Our bad words validation function
*/
add_filter(‘gform_validation’, ‘bld_custom_validation’);
function bld_custom_validation($validation_result){
$form = $validation_result[“form”];

$stop_words = array(‘SEO’, ‘1st page’, ‘optimization’, ‘link building’,’rank’,’Google searches’,’Page 2′,’Ranked higher’,
‘outsource’,
‘Sir/Madam’,
‘Sir/ Madam’,
‘Sir / Madam’,
‘Sir /Madam’,
‘long term relationship’,
‘internet marketing’,
‘internet-marketing’,
‘marketing companies’,
‘marketing company’,
‘marketing-company’,
);

$stop_id = array();

foreach($_POST as $id => $post)
{
if(bld_strpos_arr($post, $stop_words))
{
/*
* We have a match so store the post ID and initiate validation error
*/
$stop_id[] = $id;
}
}

if(sizeof($stop_id) > 0)
{
$validation_result[‘is_valid’] = false;

foreach($form[‘fields’] as &$field)
{
foreach($stop_id as $id)
{
$the_id = (int) str_replace(‘input_’, ”, $id);

if($field[‘id’] == $the_id)
{
$field[‘failed_validation’] = true;
$field[‘validation_message’] = ‘Please do not send us unsolicited messages’;
}
}
}
}

//Assign modified $form object back to the validation result
$validation_result[“form”] = $form;
return $validation_result;

}

 

You may also like...

Leave a Reply