Skip Navigation

[Resolved] Email validation using regular expression

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by puneetS-3 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1614869
Screenshot (25).png

1. I have created a signup form using User Forms. I am trying to add validation on the email field as it is accepting "special characters"@gmail.com [let's say example**@gmail.com]. I have tried this with the following code:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
	list($fields,$errors)=$error_fields;
	//$email = $fields['wpcf-user_email']['value'];
	//print_r($fields);
	if ($form_data['id']==9004111222008459)
    {
		if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $fields['wpcf-user_email']['value'])){
			$errors['wpcf-user_email']='Invalid Email';
		}
	}
	return array($fields,$errors);
}

It is returning "Invalid Email" for every email. I have checked the regular expression it is correct from my end.
Will you please look at the code and regular expression and let me know where I'm doing wrong.
I searched for the solution. But I don't find any.

2. Whenever any field is left empty the form is displaying the error message like [Please see attached screeshot]
Is there any way or any conditional output so, that I can able to display the error message "The user was not saved because of the following problems:" only when there is no field is empty.
It shouldn't be shown when the required field left empty because it is not mentioning what the error is.

Thanks

#1615329

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

1. Check the following links they suggest ways to validate an email from a specific domain:
- https://stackoverflow.com/a/20255225
- hidden link

2. You can customize the default messages in the form from the backend and you can customize the messages again in the validation hook. Check this screenshot hidden link and your screenshot, the message is the default message and asks to correct the form errors.

I hope this helps. Let me know your feedback.

#1616503

Hi Jamal,
Thanks for your help.
I followed your suggested links and Edited my code as:

function validation($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','email_validation',10,2);
function email_validation($error_fields, $form_data)
{
	list($fields,$errors)=$error_fields;
	//$email = $fields['wpcf-user_email']['value'];
	//print_r($fields);
     if ($form_data['id']==9004111222008459){
 
           $is_valid_email_domain =  validation($_POST['wpcf-user_email']);
           if(!$is_valid_email_domain) 
		   {
			    $errors['wpcf-user_email']='This E-mail domain is not allowed.';
		   }
	 }
	return array($fields,$errors);
}

I think I did something wrong but I'm not sure where
I tried to signup with the 'example@gmail.com' as well as 'example@abc.com'. But I am getting same error in both cases "This E-mail domain is not allowed."
I found the similar question here: https://toolset.com/forums/topic/split-how-to-validate-specific-email-domain-with-forms/ but it did not works in my case I am getting error for the correct domains as well.
Please help me with the working code.

Thanks

#1616763

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

The negation (!) on line 7 is not correct. Use this:

return in_array($email_domain, $domains)

Instead of this:

return !in_array($email_domain, $domains)

On line 20 you may want to say 'The email or the domain are not correct' because the function validates first the email(line 3) then validates the domain of the email.

If you still encounter issues, please allow me temporary access to check it closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
I would also need to know which URL, view, form.

#1617295

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

I believe the error on the code is the use of "wpcf-user_email" instead of "user_email". Check the name of the field in the browser network tab here hidden link

Update your code to use:

$_POST['user_email']

Instead of

$_POST['wpcf-user_email']

Let me know what you will get.

#1618243

Hi Jamal,
Yes. You are right the issue was in my code. It is working now.
I want to ask one more thing right now I'm able to validate a limited set of domains according to my code.
Is there any way so that I can able to check for the domains they exist instead of defining it manually.

Thanks.

#1618251

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Awesome 🙂 I am glad I could help.

Of course, you can modify the code to use domains from somewhere else instead of hardcoding them. You will need to build your own function, let call it "get_allowed_domains" that will return an array of allowed domains and change the code from:

$domains = array('gmail.com','yahoo.com','hotmail.com');

To:

$domains = get_allowed_domains();

It is up to you how you want to store these domains(env var, file, database, or a Toolset custom post type).

Please note, that custom code is out of the scope of the support forum. If you are not comfortable with coding, consider hiring a developer. You can check our partners here https://toolset.com/contractors/

#1618293

Okay.
Thank you!!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.