• Resolved purnamauroville

    (@purnamauroville)


    I’m trying to setup a custom email validation using the filter. I have ajax submission and validation enabled on my form. The backend code is working in the filter and the form error is coming. Yet the custom error message doesn’t show against the email field. It just shows a generic error at the top.

    Can anyone please help?

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

    function custom_email_validation($submit_errors, $form_id, $field_data_array) {
    foreach ($field_data_array as $field) {

    // Check if the 'name' key exists and if it's an email field (e.g., 'email-1')
    if (isset($field['name']) && strpos($field['name'], 'email') !== false) {
    $email = $field['value'];
    $validation_result = advanced_validate_email($email);

    if (!$validation_result['valid']) {
    // Use the field name as key in the $submit_errors array
    $submit_errors[] = [
    'field' => $field['name'],
    'message' => __( 'Invalid email: ' . $validation_result['reason'] ),
    ];
    error_log('Invalid email in field ' .$field['name'] . ' with reason ' . $validation_result['reason']);

    } else {
    error_log('Valid email: ' . $email);
    }
    }
    }

    return $submit_errors;
    }

    function advanced_validate_email($email) {
    // Remove any whitespace
    $email = trim($email);

    // 1. Syntax verification (IETF/RFC standard conformance)
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    return ['valid' => false, 'reason' => 'Invalid email format'];
    }

    list($local, $domain) = explode('@', $email);

    // 2. DNS validation, including MX record lookup
    if (!checkdnsrr($domain, 'MX')) {
    return ['valid' => false, 'reason' => 'Invalid domain MX record'];
    }

    // 3. Disposable email address detection
    $disposable_domains = [
    'yopmail.com', 'tempmail.com', 'guerrillamail.com', 'mailinator.com', '10minutemail.com'
    ];
    if (in_array($domain, $disposable_domains)) {
    return ['valid' => false, 'reason' => 'Disposable email address detected'];
    }

    // 4. Misspelled domain detection to prevent Typosquatting
    $common_domains = ['gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'icloud.com'];
    if (!in_array($domain, $common_domains) && $closest = find_closest_match($domain, $common_domains)) {
    if (levenshtein($domain, $closest) <= 2) {
    return ['valid' => false, 'reason' => "Did you mean {$closest}?"];
    }
    }

    return ['valid' => true, 'reason' => ''];
    }

    function find_closest_match($input, $words) {
    $shortest = -1;
    $closest = '';

    foreach ($words as $word) {
    $lev = levenshtein($input, $word);
    if ($lev <= $shortest || $shortest < 0) {
    $closest = $word;
    $shortest = $lev;
    }
    }
    return $closest;
    }

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.