Creating "smart" text entry fields of limited length
Posted on 2004-11-01
Here is my situation,
I am creating a content management system for my users to edit their homepage (all of the homepages are based on a simple template). I have a form in the administration area with a simple textarea which contains the current content of one of the editable texts on the homepage, when users submit that form it is entered into my mySQL database and when a visitors look at their homepage this text is pulled from the db and shown in the template... pretty standard. The problem is that I have certain text fields in the template which can only contain so-much text. If they are too long, they will overflow and break up my design.
How can I limit the amount of text entered by the user so that it doesn't overflow... Don't get me wrong, I know how to check the length of the string entered and return an error or trim off the excess... But how do I know how much text will fit in the "box" considering line breaks, long words which are "word wrapped" to the next line etc.
Here's an example to put it more simply... If I know that I need to fill up a text area which is 40 characters wide and 3 lines high... how can I figure out if a string is too long
Obviously "the<br><br><br><br>quick" might be too long while "the quick brown fox jumped over the lazy dog seventeen times yesterday" may fit, even though there are less characters... So just checking the length of the string is not enough...