Skip Navigation

[Resolved] limit and control fields input of a content form

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to validate an input field in Forms and enforce a maximum character count.

Solution: Add the following custom code:

add_filter( 'cred_form_validate', 'validate_form_20111', 10, 2 );
function validate_form_20111( $data, $form_data ) {
  $forms = array( 20111 );
  if( in_array( $form_data['id'], $forms ) ){
    list($fields,$errors)=$data;
    if ( strlen($fields['wpcf-business-brief']['value']) > 200 ) {
      $errors['business-brief'] = __('Maximum 200 characters for this field', 'your-language-domain');
    }
    $data =array($fields,$errors);
  }
  return $data;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 5 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by AmrA8547 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1207446

hi team ..

i have a no programming experience .. i built my site using divi builder and i want to limit and control fields inputs in one content submission form

- field accept only numbers
- field accept only Arabic letters
- field accept limited number of letters

i tried the following link but i think i'm doing it wrong

https://toolset.com/forums/topic/cred-form-field-character-limit-validation/

please guide me how to do it

thank you

#1207886

Hi, some of this is possible and some is not possible in Toolset.
- field accept only numbers
We offer a Number custom field that only allows numbers. This is built-in to the system and no additional programming is required. See "number.png" for an example.

- field accept only Arabic letters
This type of validation is not built-in to the Toolset software and will require custom programming that falls outside the scope of support we offer here. Our support team can offer examples showing how our custom APIs work, but building custom solutions for your site is not part of the service we offer here. Our support policy can be found here: https://toolset.com/toolset-support-policy/

If you'd like professional assistance with this type of customization, you may be able to find help from an independent contractor through our contractors portal: https://toolset.com/contractors

- field accept limited number of letters
I can help you implement this for single byte character sets. I need to know the slug of the custom field, the numeric ID(s) of the Form containing this custom field, and the maximum number of characters allowed in the field.

#1210817

ok ..

- form id: 20111
- field slug: business-brief
- maximum no. of characters : 200

thank you

#1210840

Add this code to your child theme's functions.php file, or paste it in a new code snippet in Toolset > Settings > Custom code:

add_filter( 'cred_form_validate', 'validate_form_20111', 10, 2 );
function validate_form_20111( $data, $form_data ) {
  $forms = array( 20111 );
  if( in_array( $form_data['id'], $forms ) ){
    list($fields,$errors)=$data;
    if ( strlen($fields['wpcf-business-brief']['value']) > 200 ) {
      $errors['business-brief'] = __('Maximum 200 characters for this field', 'your-language-domain');
    }
    $data =array($fields,$errors);
  }
  return $data;
}

You can change Maximum 200 characters for this field to display a different validation message.

#1210870

My issue is resolved now. Thank you!