Reputation: 5606
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:
This must be a commonly encountered issue. What is the best solution here?
Upvotes: 3
Views: 3286
Reputation: 11120
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.
Upvotes: 2