2014-10-15 4 views
5

Ich bin neu in WordPress und PHP, ich benutze Kontaktformular 7 in meiner Wordpress-Website. In diesem muss ich die email Adresse bestätigen, alle freien Domänen wie Gmail, Yahoo, etc. blockieren. Ich muss indische Telefonnummer mit Ländercode überprüfen.Benutzerdefinierte Validierung, um geschäftliche E-Mail-Adresse und Telefonnummer in Kontaktformular 7 in Wordpress zu überprüfen

Ich hatte 4 Arten von Kontaktformular, aber ich brauche diese benutzerdefinierte Validierung für nur ein Formular. Ich googelte und fand this, aber es funktioniert nicht. Jemand bitte hilf mir bei diesem Problem.

Danke.

Antwort

5

Fügen Sie den folgenden Code zu Ihrer Thema 's functions.php Datei.

// Add custom validation for CF7 form fields 
    function is_company_email($email){ // Check against list of common public email providers & return true if the email provided *doesn't* match one of them 
      if(
        preg_match('/@gmail.com/i', $email) || 
        preg_match('/@hotmail.com/i', $email) || 
        preg_match('/@live.com/i', $email) || 
        preg_match('/@msn.com/i', $email) || 
        preg_match('/@aol.com/i', $email) || 
        preg_match('/@yahoo.com/i', $email) || 
        preg_match('/@inbox.com/i', $email) || 
        preg_match('/@gmx.com/i', $email) || 
        preg_match('/@me.com/i', $email) 
      ){ 
        return false; // It's a publicly available email address 
      }else{ 
        return true; // It's probably a company email address 
      } 
    } 
    function your_validation_filter_func($result,$tag){ 
      $type = $tag['type']; 
      $name = $tag['name']; 
      if('yourid' == $type){ // Only apply to fields with the form field name of "company-email" 
        $the_value = $_POST[$name]; 
        if(!is_company_email($the_value)){ // Isn't a company email address (it matched the list of free email providers) 
          $result['valid'] = false; 
          $result['reason'][$name] = 'You need to provide an email address that isn\'t hosted by a free provider.<br />Please contact us directly if this isn\'t possible.'; 
        } 
      } 
      return $result; 
    } 
    add_filter('wpcf7_validate_text', 'your_validation_filter_func', 10, 2); // Email field or contact number field 
    add_filter('wpcf7_validate_text*', 'your_validation_filter_func', 10, 2); // Req. Email field or contact number 

Sie können Ihr gewünschtes Ergebnis mit dem obigen Code erreichen.

HINWEIS: I validiert nur Email .Sie können für den Kontakt Gleiche tun, wie ich für E-Mail tat.

Antwort für zweites Problem:

Nun, wie Sie erwähnt haben, dass Sie es wollen, dann nur für ein Formular, das Sie so etwas tun kann:

wpcf7_add_shortcode('yourid', 'wpcf7_text_shortcode_handler', true); 

Dann ein Tag wie folgt verwenden in der Form:

[yourid your-id-number-field] 

Wenn Sie die Tag-Syntax dann gehen durch diese page verstehen wollen.

Ich hoffe, es hilft Ihnen.

+0

Hey Rohil, ich habe deinen Code ausprobiert, aber es funktioniert nicht. :(Ich habe es kopiert und eingefügt und es als separates Plugin installiert. Ich kann immer noch jede E-Mail-Adresse in das [email] Feld eingeben. Was kann ich tun? – Ben

+0

Stellen Sie sicher, dass Sie dieses Plugin aktiviert haben und wenn es dann aktiviert ist bitte etwas debuggen. :) –

+0

Natürlich ist es aktiviert. :) Debuggen ist einfacher gesagt als getan. Niemals etwas in PHP getan, nur Ruby und JS .... – Ben

1

den folgenden Code verwenden, ich habe ein bisschen geändert, dass die Arbeit tun,

if ('email' == $tag->basetype || 'email*' == $tag->basetype) { 
      $arr = explode('@', $_POST[$name]); 
      if(! empty($arr[1])){ 
        $domain = strtolower(trim($arr[1])); 
      } else { 
        $domain = false; 
      } 

      if ('email*' == $type && '' == $_POST[$name]) { 
        $result['valid'] = false; 
        $result['reason'][$name] = wpcf7_get_message('invalid_required'); 
      } elseif ('' != $_POST[$name] && ! is_email($_POST[$name])) { 
        $arr = explode('@', $_POST[$name]); 
        $result['valid'] = false; 
        $result['reason'][$name] = wpcf7_get_message('invalid_email'); 
      } elseif ($domain && in_array($domain, $FREE_DOMAINS)) { 
        $result['valid'] = false; 
        $result['reason'][$name] = wpcf7_get_message('invalid_email'); 
      } 
    } 

den obigen Code einfügen in text.php Datei in Kontaktformular 7 Plugin-Modul.

+0

Es gibt absolut keine Notwendigkeit, Kern CF7 Funktionalität zu modifizieren, um dies zu erreichen. Was passiert, wenn das OP das Plugin aktualisieren will? – rnevius

+0

@mevius Pls sagen einen Weg, dies zu erreichen. –

0

Lösung:

1) Öffnen Sie Ihre Kontakt-Formular 7 Plugin text.php Datei,

contact-form-7/modules/text.php 

2) In Ihrem Browser/Text-Editor Drücken Ctrl +F, Suchen Sie dann nach dem folgenden Code.

if ('email' == $tag->basetype) { 
     if ($tag->is_required() && '' == $value) { 
      $result->invalidate($tag, wpcf7_get_message('invalid_required')); 
     } 
     elseif ('' != $value && ! wpcf7_is_email($value)) { 
     $result->invalidate($tag, wpcf7_get_message('invalid_email')); 
     }  
     } 

3) Setzen Sie den obigen Code mit

if ('email' == $tag->basetype) { 

    /*add the domain names you want to block in the $domains array*/ 
    $domains = array("gmail.com","yahoo.com","yahoo.co.in"); 
    /*explode will store the string into array 
     e.g: [email protected] 
       array(example, gmail.com)*/ 
    $udomain = explode('@', $value); 

    //select the email domain from the above splitted array 
    $email_domain = $udomain[1]; 

    // check name is 'company-email' else default validation will work 
    if('company-email' == $tag->name) { 
     //check entered value = $value exists in $domain array 
     if(in_array($email_domain, $domains)) { 
      //display error 
      $result->invalidate($tag, "Please enter your company email address"); 
     } 
    } 

    //email field is empty 
    if ($tag->is_required() && '' == $value) { 
     $result->invalidate($tag, wpcf7_get_message('invalid_required')); 
    } 

    //check basic email validation 
    elseif ('' != $value && ! wpcf7_is_email($value)) { 
     $result->invalidate($tag, wpcf7_get_message('invalid_email')); 
    } 
} 

Anmerkung: Ich

In $ Domänen Array Sie eine beliebige Anzahl von kostenlosen E-Mail-Domänen hinzufügen können

4) Aktualisieren Sie Ihr Kontaktformular 7 text.php Datei.

5) Um freie Domains zu blockieren. Verwenden Sie den folgenden Code in Kontaktformular bearbeiten,

[email* company-email class:yourclass ] //bock free domains 
    [email* your-email class:yourclass ] //default contact form 7 validation 


Note: If we use 'company-email', it will block free domains 
     If we use 'your-email'/'someothername', default contact form email validation will work. 

6) Viel Spaß mit WordPress !!!!

0

Dank @Palani Kamaraj. Aber was, wenn ich alle Sub-Domains von kostenlosen Hosting-Providern blockieren möchte, wie z. google.co. **, yahoo.co. **, usw.? Meine Array-Werte steigen weiter.

Ich fand eine andere Lösung für diese, und explodierte das Array zweimal zuerst mit '@' dann später wieder mit '.' Also in Ihrem Code oben für Schritt 3 wird der Code können Sie mit ersetzen unten

if ('email' == $tag->basetype) { 

    /*add the domain names you want to block in the $domains array*/ 
    $domains = array("gmail","yahoo","hotmail","aol","yahoo", "email", "ymail", "live", "msn"); 

    /*explode will store the string into array 
     e.g: [email protected] 
       array(example, yahoo.co.in)*/ 
    $lasta = explode('@', $value); 

    /*once again explode yahoo.**.** if the previous explode is not null 
     e.g: yahoo.co.in 
       array(yahoo, co, in)*/ 

    if ($lasta != "") { 
     $host = explode('.',$lasta[1]);      
    } 

    /* select the email domain from the above splitted array eg: yahoo */ 
    $email_domain = $host[0]; 


    // check name is 'company-email' else default validation will work 
    if('company-email' == $tag->name) { 
     //check entered value = $value exists in $domain array 
     if(in_array($email_domain, $domains)) { 
      //display error 
      $result->invalidate($tag, "Please enter your company email address"); 
     } 
    } 

    //email field is empty 
    if ($tag->is_required() && '' == $value) { 
     $result->invalidate($tag, wpcf7_get_message('invalid_required')); 
    } 

    //check basic email validation 
    elseif ('' != $value && ! wpcf7_is_email($value)) { 
     $result->invalidate($tag, wpcf7_get_message('invalid_email')); 
    } 
} 

Danke :-) Glücklich Coding :-)

Verwandte Themen