Ask your WordPress questions! Pay money and get answers fast! (more info)

Gravity Forms Custom Validation WordPress

  • SOLVED

I have created form with Gravity Forms with a text input where I want people to input their postcode/zip.

I want the form when validated to display one of the following messages:
is_valid = True: "We ship to your location"
is_valid = False: Sorry, we don't deliver to your location"

I will provide a list of the postcodes where we deliver: eg: "3380, 3381, 3382"

So:
if input_1 = 3380 = true;
If input_1 = 3379 = false;

My Form ID=4, Field ID=1
https://www.gravityhelp.com/documentation/article/gform_field_validation/

I need to full script to place in my functions.php file.

Answers (1)

2016-10-07

Reigel Gallarde answers:

try something like this:

add_filter( 'gform_field_validation_4_1', 'validate_zip', 10, 4 );
function validate_zip( $result, $value, $form, $field ) {
$valid_zip = array(3380, 3381, 3382);

if ( in_array($value, $valid_zip) ) {
$result['is_valid'] = true;
$result['message'] = 'We ship to your location';
} else {
$result['is_valid'] = false;
$result['message'] = 'Sorry, we don't deliver to your location';
}
return $result;
}


parksey18 comments:

Perfect, cheers.