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.
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
Stellen Sie sicher, dass Sie dieses Plugin aktiviert haben und wenn es dann aktiviert ist bitte etwas debuggen. :) –
Natürlich ist es aktiviert. :) Debuggen ist einfacher gesagt als getan. Niemals etwas in PHP getan, nur Ruby und JS .... – Ben