Validation of name [RESOLVED]

Hello,

I tried a new validation with regex, but it does not work. Does somebody see the problem. Thanks!

add_filter( 'gform_field_validation_23_7', 10, 4 );
function custom_validation_ends_with_Ltd( $result, $value, $form, $field ) {
  
    if (! preg_match('/Ltd.$/', $value)) {
        $result['is_valid'] = false;
        $result['message'] = 'The company name must end with Ltd.';
    }
    return $result;
}

You didn’t actually call your function. I did not check your regex, but you need to do something like this:

add_filter( 'gform_field_validation_23_7', 'custom_validation_ends_with_Ltd', 10, 4 );
function custom_validation_ends_with_Ltd( $result, $value, $form, $field ) {
    if (! preg_match('/Ltd.$/', $value)) {
        $result['is_valid'] = false;
        $result['message'] = 'The company name must end with Ltd.';
    }
    return $result;
}

Perfect. Thanks it works.

1 Like