Skip Navigation

[Resolved] Set the maxsize attribute of a cred generic text field

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

Problem:

I am trying to set the maximum number of characters for a generic text field on a CRED user form I would like to know if there is a way to achieve this without using JavaScript, possibly by using a filter or extending the PHP class responsible for the generic text field.

Solution:

The best way to accomplish this is by adding JavaScript code to the JS Editor of your form to include the desired attribute.

document.addEventListener('DOMContentLoaded', function() {
  var textField = document.querySelector('input[name="job_title"]');
  if (textField) {
    textField.setAttribute('maxlength', '50');
  }
});

Replace "job_title" with the field name that you have.

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_generic_field

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.

This topic contains 2 replies, has 2 voices.

Last updated by Saul Baizman 1 year, 7 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2597117

Hi there,

I'm trying to set the maximum number of characters for a generic text field on a CRED user form.

In straight HTML, I could simply add the "maxsize" attribute to the <input> element. I tried adding this to the [cred_generic_field] like so:

[cred_generic_field type='textfield' field='job_title' maxsize='50']

This didn't work. Without using JavaScript, is there another way I can accomplish this? Perhaps using a filter?

Alternately, can I extend the PHP class responsible for the generic text field and add this as an attribute? If so, which class should be extended?

Thanks.

Saul

#2597211

Christopher Amirian
Supporter

Languages: English (English )

Hi Saul,

The shortcode does not have such an attribute:

https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_generic_field

You will need to add a Javascript code to the JS Editor of your form to add the attribute in question.

The Javascript code will be something like this:

document.addEventListener('DOMContentLoaded', function() {
  var textField = document.querySelector('input[name="job_title"]');
  if (textField) {
    textField.setAttribute('maxlength', '50');
  }
});

But if it does not work, please share the link to the form and I will give you the correct code.

Thanks.

#2597413

Thanks for your response, Christopher. I suspected I'd have to use JavaScript. The code snippet is appreciated!

Saul