Weird Validation (exactly 13 characters required) [RESOLVED]

There should be a simple solution for this but, for the life of me, I just can’t think of it at the moment. It’s been a long year…

I have a text field that I want weird validation for. It should not be required if left blank, but if it’s not left blank, it should have exactly 13 characters - no more, no less. Does anyone know how I could do this?

Not too weird, something like the following using gform_field_validation in your theme functions.php file or a custom functions plugin should work.

Be sure to change the 231 and 1 on line 1 to match your form and field ID respectively where you want to apply the logic:

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

    if ( $result['is_valid'] && ! empty( $value ) && strlen( $value ) !== 13 ) {
        $result['is_valid'] = false;
        $result['message'] = 'Please enter a value that is 13 characters long.';
    }
    return $result;
}

The above will check if the overall validation result is valid and if the field you’re applying the logic to is neither empty nor 13 characters in length. If all of those checks are met, a validation error is returned.

If the entered value is either 13 characters long or the field is left empty no validation error would be triggered for that field.

This also assumes you don’t have the field marked already as required in the field settings, since it appears to technically be optional I would not do so and instead rely on the custom validation to handle things.

If it’s a single line text field you could also alternatively just apply a custom input mask to the field via the field settings of 13 asterisks which would force the field to only accept 13 alphanumeric characters. Less characters and the field just gets cleared out when the field loses focus and more characters won’t be able to be entered.

2 Likes

Hey Karl,

Awesome, thanks for the snippet and also for reminding me about the custom input mask option. Can’t believe I didn’t think of that! :man_facepalming: :man_shrugging:

Best,

Phil

Karl, I’m not much of a coder… is there a possibility to add an array of fields in various forms that I want to use the same function for, instead of copying/pasting the same snippet for each field?

Since the actual logic in the callback function is field and form agnostic you can just hook multiple add_filter calls, specifying different form and field IDs, into the same function e.g.

add_filter( 'gform_field_validation_231_1', 'custom_validation', 10, 4 );
add_filter( 'gform_field_validation_123_14', 'custom_validation', 10, 4 );
add_filter( 'gform_field_validation_786_90', 'custom_validation', 10, 4 );

You can’t do that for all hooks, but you can for the gform_field_validation hook and any hooks that note supporting form and field ID targeting in the hook name in their documentation.

1 Like

Awesome, thanks Karl.

Unfortunately, when I added the original snippet you shared to my child theme’s functions.php file, I got the dreaded “critical error” message. I commented out the snippet and all was fine again. Can you perhaps see an error in the snippet?

Nope, it works as is on my site. There is likely something else at play as the snippet itself is fine. The actual fatal error on your server may point towards what.

1 Like

Ok cool, I’ll check it out… I may just have inadvertently fumbled something.

Thanks again for your help, Karl!

Just to let you know, it works great now! :+1:

No idea what I did previously but all’s well that ends well. :wink:

1 Like