Not allowing spaces in input fields for users [RESOLVED]

Hello guys!

We need some help of you!

In gravity forms we have userfields, where they give us input.

There they can use spaces example: “ABC XYZ”.

We want to show the user an error, because the answer we are needing is without spaces.

We dont want just to delete the spaces, the field input should be without spaces by user.

In the same, we need also to bring the same error, when the user uses the symbols: @ and /

Thanks for your help!

Hi Erik,

This can’t be done out of the box, you need to use the gform_field_validation filter to run your own custom PHP validation function, here’s the documentation for the filter: https://docs.gravityforms.com/gform_field_validation/

Please check the following link for details and options available to insert custom PHP code: Where Do I Put This Code? - Gravity Forms Documentation

thx. we try now the following code. But now it block every Charakter “A-Z” and so on…

Can you check pls our code?

add_filter( 'gform_field_validation_17_6', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
	
	
	    if (preg_match('/(?<=[\s,.:;"\']|^)' . $word . '(?=[\s,.:;"\']|$)/', $str)) {
        $result['is_valid'] = false;
        $result['message'] = 'keine chance bro';
    }
    return $result;
}

That code isn’t going to work for a few reasons, most notably because of the undefined variables ($word and $str) you’re using. You need to be comparing your regex pattern against the field value (available in $value).

I’d do something like the following to compare the field value against a regex pattern that will only allow A-Z characters and throw an error if any numbers, spaces, or special characters are present in the value:

add_filter( 'gform_field_validation_17_6', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {


	if ( ! preg_match('/^[a-zA-Z]+$/', $value ) ) {
		$result['is_valid'] = false;
		$result['message'] = 'keine chance bro';
	}
	return $result;
}

In regards to the overall issue at hand, it is almost always a better user experience to allow the user to enter values into fields in whatever format they desire and just fix it in the background to the format you want when the values are saved.

Obviously there are exceptions to this with more specifically formatted values like emails, usernames, passwords, etc. but if it’s just a string of text you need to collect on the form, it’s better to just convert it to the format you’d like it to be in for your records, rather than putting the burden on the user to adapt to that format. Keep in mind, with every validation error the user is presented with, they will be progressively more unlikely to correct that error and fully submit the form.

1 Like

Hey @karlpotter,

your code works perfect, but we run an issue.

We have 10 fields for your script, but just 1-5 are MUST fields… the rest is optional.

So if the field is empty, the error message also appears.

Any Ideas? :frowning:

Adding an empty value check to the logic should be able to adjust for that and only compare fields that have an entered value against the regex pattern.

add_filter( 'gform_field_validation_17_6', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {


	if ( ! empty( $value ) && ! preg_match('/^[a-zA-Z]+$/', $value ) ) {
		$result['is_valid'] = false;
		$result['message'] = 'keine chance bro';
	}
	return $result;
}
2 Likes

Thanks again! Got it! Now I understand, thanks!

1 Like

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