Support Forum
Support › MB User Profile › How to validate for forbidden words?Resolved
Hi,
I want to validate certain fields like username, first and second name on a list of forbidden words.
Hence, they should not contain any of these words.
I see something about remote validation type, but it looks like there is validation on exact matches.
I need sort of 'contains' match.
How to do this?
Hello Eddy,
You can use remote validation to check the validation of a word (name). Using the regex PHP is a way to check "contains" match, please read more on the documentation
https://www.w3schools.com/php/php_regex.asp
https://docs.metabox.io/validation/#remote-validation
Note: we do not support customization code so if you cannot complete the code, please hire an expert developer to help you in this case.
Thanks Peter. I'll figure it out further. Looks good!
Peter, I'm struggling with the connection between validation rule set for the field and the action in functions.php, and back to the custom error message.
The documentation mentions
add_action( 'wp_ajax_my_action1', function () {
// Get the field value via the global variable $_GET
if ( $_GET['field_id1'] === 'something' ) {
echo 'true'; // Valid
} else {
echo 'false'; // Invalid
// or
// echo 'This value is invalid, please try again.'; // Custom error message
}
die;
} );
How to replace the $GET['field_id1'], as the action is to be performed on the current field the validation is defined for.
According to he reference you gave for the regex documentation (thanks for that!) the action should contain something like
'$pattern = "/....|....|..../";
echo preg_match($pattern, 'current field');'
So to my understanding the if-else statement needs to be replaced by the echo statement, right? Then how to connect to the current field?
Hi, I see this topic is marked as resolved, but I hope to get an answer on my last question anyway.
Hello,
The variable $GET['field_id1']
helps you to get the field value when comparing in the condition, so replacing it means nothing.
If you are not familiar with coding, I recommend contacting an expert developer to help you in this case.
Thanks.
Peter,
I understand the $GET['field_id1'] gets me the value of field1.
My question is which variable to use in the function to apply the fieldname for which the remote validation is set up.
In my case I have various fields that need to be checked for "forbidden-words" (and they are not called "field1")
https://share.getcloudapp.com/JruyWOQB
Apologies if I'm not using the proper vocabulary, but I expect you to try to understand a no-hardcore developer.
Looking forward to your help.
Can this topic be marked as "open"?
And can I pls get a response?
Thanks.
Hi Eddy,
In remote validation, all fields’s values are submitted to the server using their IDs. You can access to their values using $GET[‘field_id’]. And then perform your check with that value and return the result.
The “field_id” is the same Id as you setup the field. If you need to validate many fields, you might want to loop through list of field IDs to get values and check. Something like:
$field1_value = $GET[‘field1’];
$field2_value = $GET[‘field2’];
if ( strpos( $field1_value, ‘bad word’ ) !== false || strpos( $field2_value, ‘bad word’ ) !== false ) {
echo ‘Please use good words’;
return;
}
// Other checks if you need
echo ‘true’;
PS: type on my phone so just use the code as a starting point.