Formidable Forms – Custom Phone Validation

maspik spam

Enhance Form Submission with Custom Validation for Phone Fields in Formidable Forms

Introduction:

Formidable Forms offers robust features for creating dynamic forms on your WordPress website. To ensure data accuracy and user-friendly form submissions, it’s essential to incorporate custom validation for specific fields like phone numbers. In this guide, we’ll walk through the process of adding custom validation for phone fields in Formidable Forms using PHP and regular expressions.

Understanding the Code:

We’ll define a custom function maspik_spam_validate_formidable_phone that handles the validation logic for phone fields in Formidable Forms. This function will receive parameters representing errors, the posted field object, the posted value, and additional arguments.

Within the function:

  • We extract the field value from the posted data.
  • We check if the field value is empty or if the posted field type is not ‘phone’. If either condition is true, we return the errors array without making any modifications.
  • We define a regular expression pattern $format to validate the phone number format. This pattern ensures that the phone number contains 9 to 13 digits.
  • We use preg_match to test the field value against the defined pattern.
  • If the phone number format does not match the pattern, we add a custom error message to the errors array, indicating that the user should enter a valid phone number with 9 to 13 digits.

Implementing Your Custom Validation Logic:

The maspik_spam_validate_formidable_phone function represents a basic example of custom validation for phone fields in Formidable Forms. You can customize the regular expression pattern $format to enforce specific formatting rules for phone numbers based on your requirements. Additionally, you can extend the validation logic to include additional checks such as country-specific formats or integration with third-party validation services.

Conclusion:

By incorporating custom validation for phone fields in Formidable Forms, you can enhance the accuracy and reliability of form submissions on your WordPress website. Whether you need to enforce specific formatting rules or ensure compliance with regional standards, custom validation empowers you to create forms that collect accurate information from your users with ease.

Application:

  • Copy the Code:
/**
 * Custom validation for Formidable phone fields.
 *
 * This function validates phone numbers submitted through Formidable Forms.
 * It ensures that the phone number contains 9 to 13 digits.
 *
 * @param array  $errors        Array of errors.
 * @param object $posted_field  The posted field object.
 * @param string $posted_value  The posted field value.
 * @param array  $args          Additional arguments.
 * @return array                Modified array of errors.
 */
function maspik_spam_validate_formidable_phone($errors, $posted_field, $posted_value, $args) {
    // Extract the field value from the posted data.
    $field_value = $posted_value;

    // Check if the field value is empty or if the field type is not 'phone'.
    if (empty($field_value) || $posted_field->type !== 'phone') {
        return $errors;
    }

    // Regular expression pattern to validate phone number format (9 to 13 digits).
    $format = '/^\d{9,13}$/';

    // Check if the field value matches the defined pattern.
    if (!preg_match($format, $field_value)) {
        // Add a custom error message to the errors array.
        $errors['field' . $posted_field->id] = "Please enter a valid phone number with 9 to 13 digits.";
    }

    return $errors;
}

// Hook into Formidable Forms field validation.
add_filter('frm_validate_field_entry', 'maspik_spam_validate_formidable_phone', 10, 4);
  • Paste in Child Theme’s Functions.php: Paste the copied code into your child theme’s functions.php file. Access this file via Appearance > Theme Editor in your WordPress dashboard. It’s recommended to use a child theme to preserve modifications during theme updates.
  • Save Changes: After pasting the code snippet, save the changes to the functions.php file.

Testing and Adjustments:

  • Form Submission: Test the form submission functionality to ensure that the custom validation for phone fields is functioning correctly.
  • Validation Feedback: Verify that users receive the custom error message when entering phone numbers that do not meet the specified format requirements.
  • Customization: If necessary, customize the regular expression pattern $format and the error message within the code to align with your specific validation criteria.

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

maspik spam

Why do I feel happy when I get spammed?

Embracing Spam: How Maspik Transforms Spam into Opportunity Introduction: Spam – the bane of every website owner’s existence. We’ve all been there, wading through countless unsolicited messages, desperately trying to

Read More »