Forminator Forms – Custom Phone Number Validation in

Implementing Custom Phone Number Validation in Forminator Forms

Forminator is a powerful tool for creating interactive and user-friendly forms on your WordPress website. Ensuring that the data submitted through your forms meets specific criteria is crucial for maintaining data integrity and enhancing user experience. In this guide, we’ll explore how to add custom phone number validation to your Forminator forms using PHP.

The Code

add_filter('forminator_custom_form_submit_errors', 'maspik_validate_phone_forminator', 30, 3);

function maspik_validate_phone_forminator($submit_errors, $form_id, $field_data_array) {
    
    foreach ($field_data_array as $current) {
        $field_id = $current['name'];
        $field_value = is_array($current['value']) ? strtolower(implode(" ", $current['value'])) : strtolower($current['value']);

        if ($current['field_type'] === "phone" && !empty($field_value)) {
            // Perform custom phone number validation
            $valid = [check if number is in valid format];
            if (!$valid) {
                // Add error message to submission errors
                $error_message = "Phone number is not in a valid format";
                $submit_errors[][$field_id] = $error_message;
                return $submit_errors;
            }
        }
    }

    return $submit_errors;
}

How It Works

This code snippet uses a regular expression pattern (^\d{9,13}$) to check if the phone number has between 9 and 13 digits inclusively. If the number of digits does not fall within this range, an error message is added to the submission errors. Feel free to adjust the range (9-13 digits) according to your specific requirements.

Implementing the Code

To implement this custom phone number validation in your Forminator forms, simply add the provided PHP code snippet to your theme’s functions.php file or a custom plugin. Customize the validation logic to match your specific formatting requirements and error messages as needed.

Simplify Your Spam Protection with Maspik Plugin

If you prefer not to deal with PHP coding or custom validations, you can simplify spam protection for your Forminator Forms and other WordPress forms by using the Maspik plugin. Maspik offers a user-friendly interface with various options to block spam effectively without the need for any manual code implementation. With its intuitive settings page, Maspik allows you to configure spam prevention measures tailored to your specific requirements, ensuring a hassle-free experience while safeguarding your forms from unwanted submissions.

Get Maspik PRO for Free: Share Your Expertise!