You are viewing limited content. For full access, please sign in.

Question

Question

Rich Text Field Character Limit in Forms

asked on October 28, 2021

The max number of characters that a field can have on a template is 4000 characters.  The rich text field in forms does not let you limit the number of characters for the field like the multiline field does.  Does anyone know of a way via JavaScript or CSS that can limit the number of characters in the rich text field?  Or even a JavaScript that will check the field upon submission and give an error if it is over 4000 characters?

0 0

Answer

SELECTED ANSWER
replied on October 29, 2021

It will be tricky but it's definitely possible. It looks like the rich text field store the text in a series of nested divs. So in theory you would just target the div with a class of "rich-text-editor" then count up the number of characters contained within it and any other nested divs within it. 

You can use the following as a starting point: This should print out the length of each div's contents, while might not be perfect (haven't exactly tested it thoroughly, I'll leave that to you) but it should help get you pointed in the right direction.

$(".rich-text-editor div").each(function(ev) { 
  console.log($(this).text().length) 
});

Once you have found a way to sum of the total number of characters you can then add a function that will run on click of the submit button and check if that sum is less than 4000 characters. 

Hope that helps, Good luck!

3 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.