Hi there,
I have an employer account and would like to limit registration only for work emails.
I want to check the email address using $_POST or $fields but none of them returns the email address.
This is happening before logging in. How can I get email address? I am using AJAX.
<?php
// Put the code of your snippet below this comment.
function function_name($field_data, $form_data) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $field_data;
// validate specific form
if ( $form_data['id'] == 'form-id' ) {
// $employeremail = $_POST['user_email'];
$employeremail = $fields['user_email']['value'];
if ( empty( $employeremail ) ) {
// check if endorser email has been entered
$errors['user_email'] = 'You must provide an email';
} else {
$publicemaildomains = array("gmail", "protonmail", "yahoo", "outlook", "yahoo", "hotmail", "live", "msn", "passport");
// $current_user_id = get_current_user_id();
// $user_meta=get_userdata($current_user_id);
// $useremail = $user_meta->user_email;
$useremailsplitpos = strpos($employeremail, '@');
$useremailname = substr($employeremail, 0, $useremailsplitpos);
$endorseremailsplitpos = strpos($employeremail, '@');
$endorseremailname = substr($employeremail, 0, $endorseremailsplitpos);
$endorseremaildomain = substr($employeremail, $endorseremailsplitpos + 1);
foreach ($publicemaildomains as &$publicemaildomain) {
if (strpos($endorseremaildomain, $publicemaildomain) !== false) {
$errors['user_email'] = "Please register with a business email address";
break;
}
}
}
return array($fields,$errors);
}
}
add_filter( 'cred_form_validate', 'function_name', 10, 2 );
?>
Hi,
Thank you for contacting us and I'd be happy to assist.
I made small adjustments to the code and it seems to be working as expected on my test website:
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//validate if specific form
if ($form_data['id']==123456)
{
$employeremail = $fields['user_email']['value'];
if ( empty( $employeremail ) ) {
// check if endorser email has been entered
$errors['user_email'] = 'You must provide an email';
}
else
{
$publicemaildomains = array("gmail", "protonmail", "yahoo", "outlook", "yahoo", "hotmail", "live", "msn", "passport");
$useremailsplitpos = strpos($employeremail, '@');
$useremailname = substr($employeremail, 0, $useremailsplitpos);
$endorseremailsplitpos = strpos($employeremail, '@');
$endorseremailname = substr($employeremail, 0, $endorseremailsplitpos);
$endorseremaildomain = substr($employeremail, $endorseremailsplitpos + 1);
foreach ($publicemaildomains as &$publicemaildomain) {
if (strpos($endorseremaildomain, $publicemaildomain) !== false) {
$errors['user_email'] = "Please register with a business email address";
break;
}
}
}
}
//return result
return array($fields,$errors);
}
I hope this helps and please let me know if you need further assistance.
regards,
Waqar
Thanks for the note. It seems like you changed the parameter in the function to $error_fields.
Is this a new change or has this been always the case? Because I used examples from your documentation to write this code.
Thanks for writing back.
Although the example function in our documentation uses 'error_fields' as the first parameter's name, it doesn't matter if you change it to something else like "field_data".
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate )
I've just tested the exact code that you shared and it works on my test website too. Perhaps the form ID that you were using in place of 'form-id' wasn't correct?
My issue is resolved now. Thank you!