Skip Navigation

[Resolved] Limit number of characters in a WYSIWYG field in a Toolset Form

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

Problem:
How can we limit the amount of characters in a WYSIWYG editor of Toolset Forms?

Solution:
The issue is that you really shouldn't limit the length on rich text editors.

HTML is generated on the fly by the rich text editor so you can't really limit what is being inputted.

You could technically constrain the length of the output HTML, but doing so would likely result in malformed HTML that wouldn't run/render correctly.

It's better falling back to a simple input in the Form, if you want to limit the number of characters added to it safely.

This still requires custom code, for which you can see some examples in our forum or this very thread.

This support ticket is created 4 years, 10 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by alinaB 4 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#1452467

What I am trying to do:

I am trying to limit the number of characters that a user may input in the field named 'description' in a user form.
I have a few jQuery lines that do the job for a multi-line custom field in a CRED form, which are:

jQuery(document).ready(function ds($) {
        $('textarea[name="post_excerpt"]').attr('maxlength',160);
});

( function ds( $ ) {
    $( document ).ready( function ds(){
 
        // insert div to display char count
        $('textarea[name="post_excerpt"]').after("<div class='char-name'>Caractere rămase:</div><div class='char-count-s'>160</div>");
 
        // listen for keyup and update the count
        $('textarea[name="post_excerpt"]').keyup( function(){ 
 
            var chars_desc_scurta = 160  - $('textarea[name="post_excerpt"]').val().length;
 
            $('.char-count-s').html( chars_desc_scurta );
        });
    });
})( jQuery );

The above code works for the post excerpt and some other custom text fields I have there. Here, I need for the "description" field, which is a built-in WordPress field, it seems. I should replace 'textarea' with what? --this is in fact my main question.

#1452581

This is custom code that Toolset Supporters cannot provide.

We'd have to refer you to certified contractors here https://toolset.com/contractors/

This is because Toolset doesn't offer any counting character feature. You could suggest such a feature here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
Also, the solution doesn't implement Toolset API, which means the Toolset Support wouldn't be empowered to provide a complete code solution on this case.

To know what you need to address in the HTML (what you need to replace with "textarea") you'd have to analyse that field and accordingly seethrough the code and update the JS code.

In Toolset forms, there is however no default user field of name "Description", so I am not able to pinpoint which you mean, so I could have a look and eventually suggest the Name it uses, which is what you need to address the correct textarea[] or input.
Maybe you mean "Biographical info"?
That is a native WordPress user input - but it uses a Rich Editor, not a simple textarea.

Can you let me know which default WordPress "Description" Field you mean? If above, then that's not a text input, it's a Rich Editor, which doesn't output only one HTML tag, it is composed of several, rendered in an iframe.

#1452649

Hello Breda and thank you for your answer.
Indeed, it is the "Biographical info" field, native to WordPress, which contains a Rich Text Editor. I thought it is possible to limit the number of characters inside, in a user form, as well, like I have managed to do with custom text fields in CRED forms...
In case I do not find a solution to the problem posted above, it is not crucial, the workaround would be to define a custom user field (of the type 'textarea') and apply to it the approach mentioned above, which works flawlessly. And use this field instead of the Biographical info whenever necessary.

#1452653

I understand, but it is unrelated to Toolset.

The core issue is that you really shouldn't limit the length on rich text editors. HTML is generated on the fly by the rich text editor so you can't really limit what is being inputted.
You could technically constrain the length of the output HTML, but doing so would likely result in malformed HTML that wouldn't run/render correctly.

This might not seem what you want, but as you mention, it's better falling back to a simple input, that can't hold HTML so far, or shouldn't, so you can safely apply a restriction.

#1452859

My issue is resolved now. Thank you!