I have a front end form to create a post, but I want to validate the email field to be a specific domain. This might be a php matter, but I want to try a find a solution here, since maybe someone has already tried this.
I am trying this hook to validate a field, but it is not preventing to send the form without the domain:
function my_acf_validate_mail( $valid, $value, $field, $input_name ) {
// Bail early if value is already invalid.
if( $valid !== true ) {
return $valid;
}
if( is_string($value) && strpos($value, '@domain.com') == false ) {
return __( 'Please enter a valid email address.', 'textdomain' );
}
return $valid;
}
add_filter('acf/validate_value/name=pf_corrreo', 'my_acf_validate_mail', 10, 4);
The only thing I see in your code that might be an issue is that you should have === false as == will match 0, but this would only happen if they entered @domain.com without an address.
if( is_string($value) && strpos($value, '@domain.com') === false ) {
What type of field is this? I’m assuming it’s a text field, what I mean is, is it a sub field of another field like a group field? If it is try using the field key instead of the field name.
Thank you, in that case I’ll digg a little more on how to validate only the domain part of the email.
By the way it is an email field I am using.
are you working with a sub field? Is this field a sub field of a group or repeater? Flex field?
It is not a subfield, just an email field.
I want the user to submit their email.
The reason is this form is for logged out users, so I would like to restrict email domain, so only people in my organization can submit the form (@example.ac.cr).
Another possibility I am evaluating is to validate through LADP, so I can check if email exists in the database.