Skip to content

Instantly share code, notes, and snippets.

@alpha1
Created January 17, 2018 18:08
Show Gist options
  • Save alpha1/c224896af9f56a4fe4107e0e15f00e24 to your computer and use it in GitHub Desktop.
Save alpha1/c224896af9f56a4fe4107e0e15f00e24 to your computer and use it in GitHub Desktop.
Gravity Forms - Disallow characters from fields on many forms
<?php
/*
Use case for this is when you have certain characters like "@" that should be disallowed in certain fields. You can add a list.
This was built for text fields with job titles. Tweak as needed.
*/
add_filter( 'gform_field_validation', 'gravity_forms_validate_job_titles', 10, 4 );
function mgravity_forms_validate_job_titles( $result, $value, $form, $field ){
$forms_fields_to_check = array(
//form_id => array( $field_id, $field_id_1, $field_id_2),
30 => array( 10 ),
8 => array( 78 ),
24 => array( 78 ),
26 => array( 78 ),
32 => array( 14 ),
);
if( array_key_exists( $form['id'], $forms_fields_to_check ) ){
$form_id = $form['id'];
if( in_array( intval($field['id']), $forms_fields_to_check[$form_id] ) ){
if( strpbrk( $value, "@") !== false ){
return array( 'is_valid' => false, 'message' => "Job Titles cannot have '@' in them" );
}
}
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment