Does not contain logic

I can see that on page conditions, there is no option for ‘does not contain’ which I really need for my use case.

I am trying to work around it by doing a custom validation on a postcode field instead

This example looks like it will work, but it just needs modifying to fail if part of the ‘zips’ string arrays do not exist in the field.

In other words, I want validation to succeed if 123 999 is put in, or 123999, or 456 XYZ or 456XYZ and fail if anything else is put in.

I hope this makes some kind of sense.

add_filter( 'gform_field_validation_2_1', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array(
            '123',
            '456',
        );
 
        $zip_value = rgar( $value, $field->id . '.5' );
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            $field->set_input_validation_state( 5, false ); // Only for Gravity Forms 2.5.10 or higher.
            $result['is_valid'] = false;
            $result['message']  = 'Zip validation failed.';
        }
    }
 
    return $result;
}

Give it a try, and let me know how that goes! :smile:

add_filter( 'gform_field_validation_2_1', 'custom_zip_validation', 10, 4 );

function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array(
            '123',
            '456',
        );
 
        $zip_value = preg_replace('/\s+/', '', rgar( $value, $field->id . '.5' )); // Remove any whitespace characters from the postcode field
 
        $valid = false;
        foreach ($acceptable_zips as $zip) {
            if (strpos($zip_value, $zip) !== false) {
                $valid = true;
                break;
            }
        }
 
        if ( ! $valid ) {
            $field->set_input_validation_state( 5, false ); // Only for Gravity Forms 2.5.10 or higher.
            $result['is_valid'] = false;
            $result['message']  = 'Zip validation failed.';
        }
    }
 
    return $result;
}

Hi, thanks for this. I tried it but I can’t seem to have validation succeed.

In the context of your code if I try inputing 123 or 123 XYZ it gives me Zip validation failed.

If anyone is interested, I got this working via a chatGPT generated code of the below.

I probably wasn’t very clear on what I was after but essentially this code…

  • targets a form ID
  • targets a field ID
  • checks if any of what is put in the targeted (text) field contains any of the array strings in any part of the input string
  • if it doesn’t shows the validation message
add_filter( 'gform_validation', 'check_input_contains_strings', 10, 2 );
function check_input_contains_strings( $validation_result, $form ) {
	$target_form_id = 1;
    $target_field_id = 1;
    $target_strings = array( '123', '456', 'XYZ' );
    $field = GFFormsModel::get_field( $target_form_id, $target_field_id );
    if ( $field && $field->type == 'text' ) {
        $input_value = rgpost( 'input_' . $target_field_id );
        foreach ( $target_strings as $string ) {
            if ( stripos( $input_value, $string ) !== false ) {
                return $validation_result;
            }
        }
        // None of the target strings were found in the input value, so fail validation.
        $validation_result['is_valid'] = false;
        $field->failed_validation = true;
        $field->validation_message = "your zip code does not contain required characters";
    }
    return $validation_result;
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.