Hi there,
I’m using gform_validation to make sure a user is entering the following range of IP Addresses:
127. 0.0.0 – 127.255.255.255 127.0.0.0 /8
10. 0.0.0 – 10.255.255.255 10.0.0.0 /8
172. 16.0.0 – 172. 31.255.255 172.16.0.0 /12
192.168.0.0 – 192.168.255.255 192.168.0.0 /16
My script seems to work fine if I use the patterns individually (see below), but not if they are combined. I used https://www.regextester.com and https://www.functions-online.com/preg_match.html and it appears that the preg_match patterns are correct in either case.
I also tried including each pattern in an elseif conditional. But nothing I tried works reliably (unless I just use a single pattern).
I would appreciate any help…
function validate_private_ip($private_ip) {
$private_ip = trim($private_ip);
// The four patterns I'm trying to combine, which work fine individually...
// $pattern = "(^10\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)";
// $pattern = "(^127\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)";
// $pattern = "(^172\.16\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)";
// $pattern = "(^192\.168\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)";
// All 4 patterns...
$pattern = "(^10\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])|^127\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])|^172\.16\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])|^192\.168\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)";
if ( ! preg_match( $pattern, $private_ip ) ) {
return false;
}
return true;
}
function trgr_order_private_ip_validation( $validation_result ) {
$form = $validation_result['form'];
foreach( $form['fields'] as &$field ) {
// check for fields with this class (added via GravityForms)
if ( strpos( $field->cssClass, 'order-private-ip-validate' ) === false ) {
continue;
}
$field_value = rgpost( "input_{$field['id']}" );
$is_valid = validate_private_ip( $field_value );
if ( $is_valid ) {
continue;
}
$validation_result['is_valid'] = false;
$field->failed_validation = true;
$field->validation_message = 'Invalid entry. Must be:<br>10.0.0.0 thru 10.255.255.255;<br>127.0.0.0 thru 127.255.255.255;<br>172.16.0.0 thru 172.31.255.255; or<br>192.168.0.0 thru 192.168.255.255. ';
}
$validation_result['form'] = $form;
return $validation_result;
}
add_filter( 'gform_validation_5', 'trgr_order_private_ip_validation' );