2

I'm using rich text editors (tiny_mce) pretty widely in a web application I'm developing. An issue I can't seem to get my head around is how to limit user input so that the length of generated content fits in my database field. Character counters, which I would normally use for non rich-text content, are pretty useless because the user does not see (or know or care about) the HTML being generated behind the scenes. i.e. If I limit the input to 100 characters and the user types 99 characters they will still likely exceed the character limit because of the html tags used to structure their content behind the scenes.

The options I see are:

  • Do nothing and hope for the best. Obviously not really an option. There is a good chance that the insert will fail ungracefully when attempting to save to the db (mysql in this case).
  • Validate the length of the generated HTML and provide user with a message if it exceeds allowed length. This will be confusing to the user who, to the best of his knowledge has met the requirements for text entry. Telling him that he has exceeded the limit when, in fact, he has only entered 99 characters, would only server to confuse/enrage
  • Make the database field super large with the hope that it will never be exceeded. Seems like a bad idea...

This must be a commonly encountered issue. What is the best solution here?

JP.
  • 5,536
  • 7
  • 58
  • 100

1 Answers1

2

When the form submit, do a Javascript check on the length of the field being submitted.

As for your options:

Q: Do nothing and hope for the best.

A: Won't work. Some will will try something

Q: Validate the length of the generated HTML and provide user with a message if it exceeds allowed length.

A: I recently had a similar situation. I put a static message on the page that said the limit was 10,000, but I actually checked for 50,000. This worked for several months until someone copied and pasted out of MS Word. The user saw 5,000 characters, but it was encoded in way over 100,000

Q: Make the database field super large with the hope that it will never be exceeded. Seems like a bad idea...

A: Depending on your database and vesion, this could tolerable, not ideal, but tolerable.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72