Tell us what you are trying to do?
I have a custom field that needs to follow a specific format (XXX-XXX-0000) where X is a capital letter and 0 is a number. I have successfully added a mask to the field that forces this input from the user and that works fine. What I need to do is validate the field so that it only accepts the entry if all 12 characters are entered. At present if a user only fills in part of the field (e.g. XXX-XX) it is still accepted.
Is there any documentation that you are following?
I have tried to use the php mb_strlen command and the cred_form_validate filter as shown in this post - (https://toolset.com/forums/topic/cred-how-to-set-minmax-characters-for-text-fields/) and have added the following to a custom plugin created just for this site (rather than edit functions.php)
This is the code I have added -
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data) {
list($fields,$errors)=$field_data;
if ($form_data['id']==24){
if ( mb_strlen($fields['wpcf-po-number']['value']) < 12 ) {
$errors['wpcf-po_number']='Purchase Order Number does not match the correct format';
}
}
return array($fields,$errors);
}
How can I correctly validate the field to only be accepted if the user inputs 12 characters?
I suggest only using the strlen() function instead of the mb_strlen as this will return the length of the string in terms of bites and the strlen() will return it in terms of character length.
I have changed that and also noticed a small typo in the field name but cannot get the validation to work. The updated code is -
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data) {
list($fields,$errors)=$field_data;
if ($form_data['id']==24){
if ( strlen($fields['wpcf-po-number']['value']) < 12 ) {
$errors['wpcf-po-number']='Purchase Order Number does not match the correct format';
}
}
return array($fields,$errors);
}
I need to check if the string contains 12 characters and display the custom error if it returns false but nothing seems to work?
Thanks for looking - have changed that and it made no difference.
Would the two dashes not be counted as characters? If the order number is ABC-DEF-1234 then the field will contain 6 letters, 4 numbers and 2 dashes = 12 characters.
I have even tried putting in a function that follows the api instructions exactly that just looks for a specific value instead of looking at string length and that doesn't work either - nothing we do makes the validation pick up the field when the submit button is clicked.
So adding this code does still not validate -
add_filter('cred_form_validate','po_validation',10,2);
function po_validation($error_fields, $form_data) {
list($fields,$errors)=$error_fields;
if ($form_data['id']==24){
if ($fields['wpcf-po-number']['value']!='ABC-DEF-1234') {
$errors['wpcf-po-number']='Purchase Order Number does not match the correct format';
}
}
return array($fields,$errors);
}
The mask only accepts 6 uppercase letters followed by 6 numbers. If you look at the Custom JS section for the Post Form you should see the javascript function that forces this.
I did try removing the mask to see if it then allowed the validation filter to run but it made no difference.
It is now working but using the code I posted above that will only accept ABC-DEF-1234
I have been playing with this and think it is now working after changing the < to !=
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data) {
list($fields,$errors)=$field_data;
if ($form_data['id']==24){
if ( strlen($fields['wpcf-po-number']['value']) != 12 ) {
$errors['wpcf-po-number']='Purchase Order Number does not match the correct format';
}
}
return array($fields,$errors);
}
It was like this before '12' with the quotes, however if we remove the quotes it works fine. This means that it was comparing the results to the string 12 instead of an integer 12.