· 8 years ago · Aug 10, 2018, 03:44 PM
1/**
2 * function to validate email address
3 * check against array of blacklisted domains
4 */
5function gf_email_validation($validation_result) {
6 // Set the $validation_result object to $form
7 $form = $validation_result["form"];
8 // Get the value the user entered for the email field (name of input)
9 $email = $_POST["input_2"];
10 $email_blacklist = array("yahoo.com", "hotmail.com", "aol.com", "msn.com", "verizon.com", "comcast.com", "live.com", "gmx.com", "outlook.com", "gmail.com");
11 //explode will store the string into array, e.g: example@gmail.com
12 $udomain = explode('@', $email);
13 //select the email domain from the above splitted array
14 $email_domain = $udomain[1];
15
16 if (in_array($email_domain, $email_blacklist)) {
17 // set the form validation to false
18 $validation_result["is_valid"] = false;
19 // specify the invalid field and provide custom validation message
20 $form["fields"][1]["failed_validation"] = true;
21 $form["fields"][1]["validation_message"] = __('You must enter a business email address.', 'spx');
22 }
23 // update the form in the validation result with the form object you modified
24 $validation_result["form"] = $form;
25 return $validation_result;
26}
27add_filter('gform_validation', 'gf_email_validation', 20, 2);