Last updated on September 25, 2023

ACF email field validation

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Validating Email Fields in ACF.

The code snippet below demonstrates how to validate an email field created with Advanced Custom Fields (ACF) in WordPress. This code can be useful when you want to ensure that the email entered by the user is in a valid format before saving it to the database.

function wpsnippets_validate_email_field($valid, $value, $field, $input_name) {
    if ($value && !is_email($value)) {
        $valid = false;
        $field['message'] = 'Please enter a valid email address.';
    }
    return $valid;
}
add_filter('acf/validate_value/type=email', 'wpsnippets_validate_email_field', 10, 4);

In this code, we define a custom validation function wpsnippets_validate_email_field that accepts four parameters: $valid, $value, $field, and $input_name. The $valid parameter represents the current validation status, $value contains the value entered by the user, $field contains the ACF field object, and $input_name is the name attribute of the input field.

Inside the function, we check if the $value is not empty and if it is not a valid email address using the is_email() function provided by WordPress. If the email is invalid, we set $valid to false and update the $field['message'] to display a custom error message.

Finally, we use the add_filter() function to hook our validation function to the acf/validate_value/type=email filter, which is specific to email fields created with ACF. This ensures that our validation function is called when an email field is being validated.

By using this code snippet, you can easily add email field validation to your ACF forms and provide feedback to users if they enter an invalid email address.

Examples

Example 1: Basic Email Field Validation

This use case demonstrates how to validate an email field using Advanced Custom Fields (ACF) in WordPress. The code example below shows how to add a custom validation rule to an email field.

function wpsnippets_validate_email_field($valid, $value, $field, $input_name) {
    if ($value && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
        $valid = false;
        $field['message'] = 'Please enter a valid email address.';
    }
    return $valid;
}
add_filter('acf/validate_value/type=email', 'wpsnippets_validate_email_field', 10, 4);

In this code, we define a custom validation function wpsnippets_validate_email_field that takes four parameters: $valid, $value, $field, and $input_name. The function checks if the $value is a valid email address using the filter_var function with the FILTER_VALIDATE_EMAIL filter. If the email is not valid, we set $valid to false and update the error message in $field['message']. Finally, we return the updated $valid value.

We then use the add_filter function to hook our custom validation function to the acf/validate_value/type=email filter, which is specific to email fields in ACF. This ensures that our validation function is called whenever an email field is validated.

Example 2: Custom Error Message

This use case demonstrates how to provide a custom error message for email field validation in ACF. The code example below shows how to modify the error message displayed when an invalid email address is entered.

function wpsnippets_custom_email_error_message($message, $field) {
    if ($field['type'] === 'email') {
        $message = 'Please enter a valid email address.';
    }
    return $message;
}
add_filter('acf/validate_value/message', 'wpsnippets_custom_email_error_message', 10, 2);

In this code, we define a custom filter function wpsnippets_custom_email_error_message that takes two parameters: $message and $field. We check if the $field type is email and update the $message to our custom error message if it is. Finally, we return the updated $message value.

We use the add_filter function to hook our custom filter function to the acf/validate_value/message filter, which allows us to modify the error message displayed for all field types. Within our filter function, we specifically target the email field type to customize the error message for email fields only.

Example 3: Conditional Email Field Validation

This use case demonstrates how to conditionally validate an email field based on the value of another field in ACF. The code example below shows how to validate an email field only if a checkbox field is checked.

function wpsnippets_conditional_email_validation($valid, $value, $field, $input_name) {
    $checkbox_value = get_field('checkbox_field');
    if ($checkbox_value && $value && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
        $valid = false;
        $field['message'] = 'Please enter a valid email address.';
    }
    return $valid;
}
add_filter('acf/validate_value/type=email', 'wpsnippets_conditional_email_validation', 10, 4);

In this code, we define a custom validation function wpsnippets_conditional_email_validation that takes four parameters: $valid, $value, $field, and $input_name. We retrieve the value of a checkbox field using the get_field function and store it in $checkbox_value. If the checkbox is checked ($checkbox_value is truthy) and the email field has a value, we proceed with the email validation as before. Otherwise, we skip the validation.

We use the add_filter function to hook our custom validation function to the acf/validate_value/type=email filter, which is specific to email fields in ACF. This ensures that our validation function is called whenever an email field is validated. By checking the value of the checkbox field, we conditionally apply the email validation.

Last updated on September 25, 2023. Originally posted on October 9, 2023.

Leave a Reply

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