Skip Navigation

[Resolved] Using custom validation to check the string length of a field

This support ticket is created 7 years, 4 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 2 voices.

Last updated by Shane 7 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#552522

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?

Is there a similar example that we can see?

What is the link to your site?

#552594

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Thank you for contacting our support forum.

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.

Try this and let me know if it works.

Thanks,
Shane

#552781

Hi Shane

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?

#553065

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Would you mind providing me with admin access to the form so that I can check on this for you ?

The private fields will be enabled for your next response.

Thanks,
Shane

#553639

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

You gave a format but the number is 10 characters long. which should always fail validation.

Replace this

strlen($fields['wpcf-po-number']['value']

With

strlen($_POST['wpcf-po-number'])

It could be that we are not getting the value from the $fields list.

Thanks,
Shane

#553674

Shane

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);
}
#553943

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

I tried to test the field but its not accepting any characters that i'm entering.

Could you check on this and let me know.

Thanks,
Shane

#553945

Hi Shane

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.

Thanks

Pete

#553952

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screen Shot 2017-07-28 at 9.49.10 AM.png

Hi Peter,

I test the issue again its working fine. See Screenshot.

Does this not happen for you ?

Thanks,
Shane

#553962

Shane

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);
}
#553976

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

I believe the issue is with the 12 🙂

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.

Try it now as it should work.

Thanks,
Shane