Forum Moderators: open

Message Too Old, No Replies

Limiting amount of text in a form field

         

greencode

8:00 pm on Jul 4, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Now I've seen it where you can limit the amount of words of even characters Word Counter [javascript.internet.com] but is there a way of limiting it to the size of the text field.

I have a field that I need users to fill in but the title cannot spill over onto two lines so I need to limit what they input. If I add a character count this doesn't necessarily work because some characters are bigger than others.i is smaller than w in most fonts. Therefore is there a way of doing this or do I just have to use a fixed width font such as courier?

I hope all that makes sense and thanks in advance for any help.

penders

9:57 pm on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A function that returns the actual pixel width of a string in a particular font?!

This is possible in a desktop application when the environment (and importantly the fonts) is known, but on a browser on the www where there is a lot of unknown then trying to be pixel perfect is a mighty tricky affair!

You could try and approximate it. Perhaps give each character a width based on a known font/size and calculate the overall width once the user has submitted the text (form validation). There may be a gfx library that will do this more accurately? But there is no guarantee that the avg web user will have the font you have based your approximation on.

Even using a fixed width font may not be the answer - what size?!

Alternatively, allow your users to enter any title they like (restricted to a sensible max number of chars). Then simply restrict the area in which the title appears (overflow:hidden) so that it won't spill onto two lines. If the title doesn't fit, give your users the option to edit it.

... welcome to WebmasterWorld! :)

greencode

8:32 am on Jul 5, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for the help. Yeah, it certainly is a tricky one and we do have a Preview link so they will be able to see it before it goes live. I like your suggestion of limiting the area and then they will be able to see that not all their text fits so they will need to edit it.