Skip Navigation

[Resolved] Split: How to validate specific email domain with forms

This thread is resolved. Here is a description of the problem and solution.

Problem: How to validate specific email domain with forms

Solution: Well - Toolset Forms offers hook for validating the post before you save it. You can use Form hook cred_form_validate to validate your form.

You can find the proposed solution, in this case, with the following reply: https://toolset.com/forums/topic/split-how-to-validate-specific-email-domain-with-forms/#post-1203558

Relevant Documentation: => https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by patrickM-3 2 years, 5 months ago.

Assigned support staff: Minesh.

Author
Posts
#1203555

Thanks, Minesh, that works great.

I also need to do a couple of specific things during the validation:

1) I need to make sure that emails entered via the form use one of two possible domains: domainone.com or domaintwo.com for example. minesh@domaintwo.com would be accepted but minesh@google.com would generate a form error. Can you help me with the PHP for this bit?

2) If someone successfully submits this form, I need to allow them to return and modify their entries in the future. I am NOT creating wordpress users with these emails. I am only storing email addresses in a custom post type. When a user returns to the site, I would like to automatically show them the field values for the form they submitted already and allow them to make any changes/additions.

If the validation code above, it looks like I could modify the part where the error for a pre-existing email could be modified to redirect the user to the edit existing post version of the same form. Perhaps it would make sense to break this into 2 steps; first, have the user enter their email address and then re-direct them to either a new entry form or edit existing form depending on whether the email is in the database or not.

If that is the best approach, I will need to make a new form that contains only the email address and then, in the validation for that form add a redirect in the validation code to either new entry or edit existing based on the email. I will also need to perform the validation to screen out any disallowed domains at that point.

Would it make more sense to add JavaScript to write either the email or the post ID of the submission to a cookie and use that to redirect the user to the edit version of the same form? Or, is there a preferred method for this?

#1203558

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - to validate the specific email domain, you should try to use the following function:

For example - use the following function:

function validate_email_domain($email){
  if (!preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $email)) return false;

  $domains = array('gmail.com','yahoo.com','hotmail.com');
  list(, $email_domain) = explode('@', $email, 2);
  return !in_array($email_domain, $domains);
}

add_filter('cred_form_validate','func_validate_email_with_domain',10,2);
function func_validate_email_with_domain($error_fields, $form_data){
  
    //field data are field values and errors
  
    list($fields,$errors)=$error_fields;
  
     if ($form_data['id']==9999){

           $is_valid_email_domain =  validate_email_domain($_POST['wpcf-EMAIL-FIELD-SLUG']);
    if($is_valid_email_domain) {
          
        $args = array(
                    'meta_query' => array(
                        array('key' => 'wpcf-EMAIL-FIELD-SLUG',
                              'value' => $_POST['wpcf-EMAIL-FIELD-SLUG']
                            )),
                    'post_type' => 'your-post-type',
                    'posts_per_page' => -1
                    );
            $posts = get_posts($args);
  
  
        //check if birthday value is already on the database
  
        if (count($posts) > 0){
  
            //set error message for my_field
  
            $errors['wpcf-EMAIL-FIELD-SLUG']='This E-mail already registered.';
  
        }

    }else{
		  $errors['wpcf-EMAIL-FIELD-SLUG']='This E-mail domain is not allowed.';
	}
  
    }
  
    return array($fields,$errors);
  
}

Where:
- Replace 9999 with your original Form ID
- Replace EMAIL-FIELD-SLUG with original email field slug
- Replace your-post-type with your original post type
- Replace the $domains with the values of domains which you want to allow.

#1204034

Thank you Minesh. It works for finding duplicate emails, but it does not correctly limit the domains to those in the array.

I have a custom post type of Filtered Email and a custom field for that post type called Domain Limited Email. The slug appears to work as the check for existing record part of the code functions correctly.

Here's the actual code I'm using:

function validate_email_domain($email){
	if (!preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $email)) return false;
	$domains = array('domain1.com','domain2.com','domain3.biz');
	list(, $email_domain) = explode('@', $email, 2);
	return !in_array($email_domain, $domains);
}
 
add_filter('cred_form_validate','func_validate_email_with_domain',10,2);

function func_validate_email_with_domain($error_fields, $form_data){
	//field data are field values and errors
	list($fields,$errors)=$error_fields;
	if ($form_data['id']==214){
		$is_valid_email_domain =  validate_email_domain($_POST['wpcf-domain-limited-email']);
		if($is_valid_email_domain) {
			$args = array(
				'meta_query' => array(
					array('key' => 'wpcf-domain-limited-email',
						  'value' => $_POST['wpcf-domain-limited-email']
						 )),
				'post_type' => 'filtered-email',
				'posts_per_page' => -1
			);
			$posts = get_posts($args);
			//check if email is already in the database
			if (count($posts) > 0){
				//set error message for my_field
				$errors['wpcf-domain-limited-email']='This E-mail already registered. Please use the link you received via email to update your contest entry.';
			}
		}else{
			$errors['wpcf-domain-limited-email']='Not an authorized, work email.';
		}
	}
	return array($fields,$errors);
}

If I submit an entry using either bob@domain1.com or bob@domain4.com they both return success. Perhaps there is an error with the line:

list(, $email_domain) = explode('@', $email, 2);

#1204048

Found it:

return !in_array($email_domain, $domains);

should be

return in_array($email_domain, $domains);
#1376611

Hi Minesh,

My client has updated this site and now wishes to add a large number of approved email domains. Isn't there a way to do validation within Toolset, using Toolset logic? I need to be able to create a custom post type for Approved Domains, which the client can edit and maintain.

Then, I need to be able to redirect entries based on whether their email domain is one of these custom post types or not. It would certainly be great if there was a validation editor for forms, similar to that for views. Is there any way to obtain this functionality that would preclude writing any custom PHP functions?