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.

MASPIK - Spam Blacklist

Maspik is an anti-spam plugin that blocks spam submissions through your contact forms, comments, and registration pages. With Maspik, you can blacklist specific words, emails, phone validation, IPs, countries, and languages to prevent spam from being delivered to your inbox.

More from Maspik Blog

Changes at Maspik Plugin

The first stable of Maspik Version 1.0.0 Dear Maspik Users, We wanted to take a moment to share some important updates regarding the Maspik plugin. First and foremost, we want

Read More »