Add IP Address Validation to forminator form field
-
Hi,
I need help to add validation for a field that takes IP Address as an input. Can someone please guide me on how I can get this done asap.
-
Hello @himanshu5898 !
Hope you’re having a good day!
To add IP validation for a text field, please add this snippet as a new .php file to your site. The filename doesn’t matter, as long as the extension is .php and the file is placed in wp-content/mu-plugins directory:
<?php add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) { $target_form_id = 13; $text_field = 'text-1'; if ( $target_form_id != $form_id ) { return $submit_errors; } $submitted_values = wp_list_pluck( $field_data_array, 'value', 'name' ); if ( ! isset( $submitted_values[ $text_field ] ) || ! filter_var( $submitted_values[ $text_field ], FILTER_VALIDATE_IP ) ) { $submit_errors[][ $text_field ] = esc_html__( 'Invalid IP address.' ); } return $submit_errors; }, 10, 3 );
There are two variables that will need to be adjusted:
$target_form_id = 13; $text_field = 'text-1';
Please make sure those match your form’s ID (same as the value in the shortcode or the id parameter in the form’s edit screen URL) and the field name – you can find it in the curly braces, for example {text-1}.
Kind regards,
PawelHi,
So I tried adding the code. I replaced required fields. It is working fine when I have a proper IP. But I am getting some error above my form. I only need to show the error saying ‘invalid IP’. I do not understand why I am getting another error. I have uploaded screenshots on a link below. You can take a look at the same. One is of the same code with changes, other is the one webpage where I have added the form.Can you please take a look once and guide me further.
-
This reply was modified 3 years, 6 months ago by
himanshu5898.
Thanks for response!
I’d suggest to not use spaces in .php file names but that’s just a side-note.
As for the issue:
You got an “Invalid IP address” error right below the field which is the error that this code adds in case of invalid IP.
The other error is from the form itself and is a general validation error as the IP field is set as required and the custom code marks it as invalid. If it’s only showing with invalid IP that would be expected outcome as the form can only fully validate (and not show any general errors) if all its required/validated fields are properly validated.
Kind regards,
AdamCan I not hide the message using some css class? Or is there no other way to get rid of it?
(Urgent)
Hello Guys,
I have added the normal IP validation for my form. Can someone please help me to add validation for private IP as well. I do not want to allow private IP address as well.This is super urgent and I need to get it done asap.
Can I not hide the message using some css class? Or is there no other way to get rid of it?
If you do that it would hide all other errors, the validation triggers the warning on top of the form and in the field level what you defined on:
$submit_errors[][ $text_field ] = esc_html__( 'Invalid IP address.' );
I have added the normal IP validation for my form. Can someone please help me to add validation for private IP as well. I do not want to allow private IP address as well.
Can you test this version?
<?php add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) { $target_form_id = 4826; $text_field = 'text-1'; if ( $target_form_id != $form_id ) { return $submit_errors; } $submitted_values = wp_list_pluck( $field_data_array, 'value', 'name' ); function is_public_ip($ip=NULL) : bool { return filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) === $ip ? TRUE : FALSE; } if ( ! isset( $submitted_values[ $text_field ] ) || ! is_public_ip( $submitted_values[ $text_field ] ) ) { $submit_errors[][ $text_field ] = esc_html__( 'Invalid IP address.' ); } return $submit_errors; }, 10, 3 );
You will find more references on https://stackoverflow.com/questions/13818064/check-if-an-ip-address-is-private/13818647
Let us know the result you got.
Best Regards
Patrick FreitasI hope you are doing well and safe!
We haven’t heard from you in a while, I’ll mark this thread as resolved.
Feel free to let us know if you have any additional questions or problems.
Best Regards
Patrick Freitas -
This reply was modified 3 years, 6 months ago by
- The topic ‘Add IP Address Validation to forminator form field’ is closed to new replies.