Email field validation issue.
-
Hi,
I am implementing simple email validation for all email fields in the form.
To achive it I have followed the Ninja Forms doc for reference and used ninja_forms_submit_data filter hook.I am facing an issue with the non-required email field. after submission, if the condition is not fulfilled, I passed a custom error message for the respective email field.
// Ninja form email field validation. add_filter( 'ninja_forms_submit_data', 'test_ninja_forms_submit_data', 10, 1 ); function test_ninja_forms_submit_data( $form_data ) { foreach ( $form_data['fields'] as $field ) { // Field settigns, including the field key and value. $value = $field['value']; if ( preg_match( '/@.+\./', $value ) && is_email( $value ) && ( strlen( $value ) < 20 ) ) { $email = $value; $field_id = $field['id']; $form_data['errors']['fields'][ $field_id ] = 'Email lenght must be grater then 20 character, your email length is ' . strlen( $value ) . ' char'; } } return $form_data; }
So, when I provide an incorrect email that won’t pass the validation rule it will show the error. After providing a correct email that will pass the validation test for the required field, it’ll accept it and show a tickmark.
This happens when the field is ‘required’
However for a non-required field, when you try to after providing an email the submission of the form stops.
Can you please explain why does the code works fine for the required field and not for the non-required email field?- This topic was modified 3 years, 1 month ago by .
- This topic was modified 3 years, 1 month ago by .
- This topic was modified 3 years, 1 month ago by .
The page I need help with: [log in to see the link]
- The topic ‘Email field validation issue.’ is closed to new replies.