Require a house number in a submitted address

🧪

Experimental Snippet

This snippet was discovered in the wizard’s chaotic laboratory — an unpolished experiment, conjured to solve a niche challenge. Use at your own risk.

Instructions

Code

Filename: gw-require-house-number-in-address.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
/**
 * Gravity Wiz // Gravity Forms // Require a house number in a submitted address
 * https://gravitywiz.com/
 *
 * Experimental Snippet 🧪
 *
 * Instructions:
 *  * See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *  * Customize FORMID and FIELDID accordingly.
 */
add_filter( 'gform_field_validation_FORMID_FIELDID', function( $result, $value, $form, $field ) {
	if ( $field->type !== 'address' ) {
		return $result;
	}

	$street = trim( rgar( $value, $field->id . '.1' ) );

	// Look for a standalone number at the beginning or end of the Address Line 1 value
	if ( ! preg_match( '/^\d+\s+/', $street ) && ! preg_match( '/\s+\d+$/', $street ) ) {
		$result['is_valid'] = false;
		$result['message']  = empty( $field->errorMessage ) ? 'Please provide an address with a house number.' : $field->errorMessage;

		$field->set_input_validation_state( 1, false );
	}

	return $result;
}, 10, 4 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.