IPv4 + CIDR Validation + Live Mask

Adds live and server-side validation for IPv4 addresses with optional CIDR notation.

Supported formats:

  • Standard IPv4: 192.168.1.1
  • CIDR notation: 192.168.1.1/24

Instructions

  1. Add this snippet to your site. See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/.
  2. Add ‘validate-ipv4’ to the “Custom CSS Class” field setting of the Single Line Text field(s) you want to validate.

Code

Filename: gw-validate-ipv4-number-field.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 * Gravity Wiz // Gravity Forms // IPv4 + CIDR Validation + Live Mask
 * https://gravitywiz.com/
 *
 * Adds live and server-side validation for IPv4 addresses with optional CIDR notation.
 *
 * Supported formats:
 * - Standard IPv4: 192.168.1.1
 * - CIDR notation: 192.168.1.1/24
 *
 * Instructions:
 * 1. Add this snippet to your site. See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/.
 * 2. Add 'validate-ipv4' to the "Custom CSS Class" field setting of the Single Line Text field(s) you want to validate.
 */
class GW_IPv4_Mask_Validation {

	public function __construct() {

		add_action( 'gform_enqueue_scripts', array( $this, 'enqueue_script' ) );
		add_filter( 'gform_field_validation', array( $this, 'validate_ipv4' ), 10, 4 );

	}

	/**
	 * Frontend Live Validation + Input Mask
	 */
	public function enqueue_script() {
		?>

		<script>
		(function() {

			function isIPv4Valid(value) {

				var regex = /^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\/([0-9]|[12][0-9]|3[0-2]))?$/;

				return regex.test(value);

			}

			function sanitizeInput(value) {
				return value.replace(/[^0-9./]/g, '');
			}

			document.addEventListener('input', function(e) {

				var input = e.target;

				if (!input.closest('.gf-ipv4-mask')) return;

				input.value = sanitizeInput(input.value);

				validateField(input);

				// Mobile numeric keypad
				input.setAttribute('inputmode', 'numeric');

			});

			document.addEventListener('blur', function(e) {

				var input = e.target;

				if (!input.closest('.gf-ipv4-mask')) return;

				validateField(input);

			}, true);

			function validateField(input) {

				var container = input.closest('.gfield');
				var value = input.value.trim();

				if (value === '') {
					container.classList.remove('gfield_error');
					return;
				}

				if (!isIPv4Valid(value)) {
					container.classList.add('gfield_error');
				} else {
					container.classList.remove('gfield_error');
				}

			}

		})();
		</script>

		<?php
	}

	/**
	 * Server-side Validation
	 */
	public function validate_ipv4( $result, $value, $form, $field ) {

		if ( $field->type !== 'text' ) {
			return $result;
		}

		if ( strpos( $field->cssClass, 'gf-ipv4-mask' ) === false ) {
			return $result;
		}

		if ( rgblank( $value ) ) {
			return $result;
		}

		$value = trim( $value );

		$regex = '/^
		(
			(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)
			\.
		){3}
		(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)
		(\/([0-9]|[12][0-9]|3[0-2]))?
		$/x';

		if ( ! preg_match( $regex, $value ) ) {

			$result['is_valid'] = false;
			$result['message']  = 'Please enter a valid IPv4 address (example: 192.168.1.1 or 192.168.1.1/24)';

		}

		return $result;

	}

}

new GW_IPv4_Mask_Validation();

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.