Skip to main content

This code hooks into the gform_validation filter, which is called when a Gravity Forms form is submitted. It then loops through all the form fields and checks if they are of type email. If a field is of type email, it checks if the value is not empty and if it is in a valid email format using filter_var.

If the email format is invalid, it sets the failed_validation property of the field to true and sets a validation message. It also sets the is_valid property of the $validation_result to false to indicate that the form is not valid. Finally, it returns the modified $validation_result.

Note that this code assumes that the field ID of the email field is input_{field_id}. You’ll need to replace {field_id} with the actual ID of your email field.

add_filter( 'gform_validation', 'validate_email_format' );
function validate_email_format( $validation_result ) {
$form = $validation_result['form'];
foreach( $form['fields'] as &$field ) {
if( $field->type == 'email' ) {
$value = rgpost( "input_{$field->id}" );
if( !empty( $value ) && !filter_var( $value, FILTER_VALIDATE_EMAIL ) ) {
$field->failed_validation = true;
$field->validation_message = "Please enter a valid email address.";
$validation_result['is_valid'] = false;
}
}
}
$validation_result['form'] = $form;
return $validation_result;
}