I’m using the Address field in my forms so there is no way of setting a mask for the individual sub fields in the Address field. I need to validate the zip code field such that, upon submission, if the field contains anything other than 5 numerals, the form should show an error message just like any other required field. I’m using United States address format and no need for the zip+4 format.
I see you’ve used gform_field_validation_3_55.5 as the filter, have you tried gform_field_validation_3_55?
From what I see in the docs (and from what I’ve used myself in the past) it doesn’t work with the subID for the specific field inside a field-group and should be Form ID + Field ID. So the overall field ID (55 in this case).
Well, that’s one step closer to the solution either way
In the code you pasted above, the ‘demo zips’ are still mentioned (which are both 3 numbers). Is that the code you’re indeed using? Because that situation doesn’t resemble what you’re trying to achieve.
You could use ctype_digit + strlen inside the filter to check if the given value is indeed (5) digits:
That worked, but what if I want to use the same filter for more than one form on my site? I repeated the filter and updated the form but with both filters in my functions.php, the site was not accessible. What would the syntax look like for two forms / 2 field IDs?
Then you’d add a seperate add_filter for that form + field ID (and I’d select it at field type address inside the function instead of the field ID to keep it compact & more scalable).
The add_filter should be used once per form. The first string is the filtername, the second string is the name of the function that should be used. In yours the first two strings are filter names and the third is the functionname. Since the second string is recognized by WordPress as the function and that function doesn’t exist: it doesn’t work.
Yes - I believe all you need to do is add an additional line like:
add_filter( ‘gform_field_validation_3_55’, ‘custom_zip_validation’, 10, 4 ); for each zip field in the form (in this example, ‘3’ is form ID and ‘55’ is the field ID of address field) replacing the " _55" with the field ID of additional address field.
If you are using a field for a zip code as a stand-alone and not part pf a address field, the function itself might be a little different.
@jcallanan Thank you. I guess I was looking for a more simpler solution rather then write ‘add_filter’ 24 times. I have 24 address fields and each of the zip codes need validation.
Will there be any issues if I write 24 ‘add_filter’ hooks?
Actually that’s quite possible, just remove the field ID (to apply it to a single form) and use the first gist where the ‘address’-field type is mentioned.
The add_filter would just be added once for the whole form and all address-fields in that form would have that same filter for the zipcode field.
Anyway, if you check the gist for zipcode-3.php (just added it) you’ll see what I mean