Bricksbuilder Forms: Custom Phone Validation

Creating Custom Phone Number Validation in Bricksbuilder Forms

Are you using BricksBuilder to create stunning forms for your website? Ensuring that the data submitted through your forms is accurate and valid is essential for maintaining a seamless user experience. In this guide, we’ll walk through the process of adding custom phone number validation to your Bricksbuilder forms using PHP.

Why Custom Phone Number Validation?

Phone numbers are a critical piece of contact information collected through forms. Implementing custom validation allows you to enforce specific formatting rules or restrictions based on your requirements. In this example, we’ll focus on validating phone numbers to ensure they adhere to a specific format, specifically targeting phone numbers from the USA.

The Code

add_filter('bricks/form/validate', 'maspik_custom_validate_bricks_form', 10, 2);

function maspik_custom_validate_bricks_form($errors, $form) {
    $settings = $form->get_settings();
    $form_fields = $settings['fields'];
    $values = $form->get_fields();

    // Regular expression pattern for USA phone numbers
    $usa_phone_regex = '/^(?:\+?1[-.\s]?)?\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})$/';

    // Perform spam validation for each form field
    foreach ($form_fields as $field) {
        $field_id = $field['id'];
        $field_value = $form->get_field_value($field_id);

        if ($field['type'] === 'tel' && !empty($field_value)) {
            // Check if the phone number matches the USA format
            if (!preg_match($usa_phone_regex, $field_value)) {
                $errors[] = "Phone is not in a valid USA format";
                return $errors;
            }
        }
    }

    return $errors;
}

How It Works

This PHP code snippet hooks into the bricks/form/validate filter provided by Bricksbuilder. It iterates through the form fields, targeting fields of type tel (telephone). For each telephone field, it validates the entered phone number against a regular expression pattern designed to match common USA phone number formats. If the entered phone number does not match the expected format, an error message is added to the form submission errors.

Implementing the Code

To implement this custom phone number validation in your Bricksbuilder forms, simply add the provided PHP code snippet to your theme’s functions.php file or a custom plugin. Customize the regular expression pattern $usa_phone_regex as needed to match your specific formatting requirements.

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 Bricksbuilder 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.

Block spam in WP with Maspik

Choose your plan

14-day money-back guarantee for any plan | NO RISK

$19

/ Yearly

$59

/ Yearly

(~5.9$ per site)

$129

/ Yearly

(~1.29$ per site)

$299

/ Yearly

(~0.3$ per site)

Contact Form 7 Spam From Real Gmail Addresses

Contact Form 7 Spam From Real Gmail Addresses

If your Contact Form 7 submissions are coming from real-looking Gmail addresses, that does not automatically mean the submissions are legitimate. Modern spam campaigns often use: This is one of the biggest reasons traditional spam filtering assumptions no longer work

Read More »

Is Akismet Still Relevant in 2026?

The Difference Between Old Comment Spam and Modern WordPress Spam For nearly two decades, Akismet Anti-Spam has been one of the most recognizable plugins in the WordPress ecosystem. If you have used WordPress long enough, you have probably seen it

Read More »

Get Maspik PRO for Free: Share Your Expertise!