Forum OpenACS Development: Improve user-experience: character limit on form input fields

Request notifications

Improve user-experience: character limit on form input fields

When I try a form, I always get tempted to overfill title fields... Sometimes, there is no form validation and I get an Oracle error. This is too frustrating. I know that one solution is validating the forum elements (server-side). But it's also frustrating to get one or more error messages when you think you were finished. Sometimes the problem is not the database itself but a system/information/whatever requirement about a short char limit (i.ex. telephone number... or something like that)
Column with that restriction are, usually, keys, some titles (not forum messages titles), group name, forum name...

It could be nice if the user could be warned somehow before posting (client-side). Note that that is also usefull for mandatory fields.

The simplest idea is using the MAXLENGTH value on input fields, so the browser didn't let the user write any more.

We have talked about that today on irc, I'm going to write here a summary of the best ideas, in fact, all of them...

–Maxlength value is not necessarily better its just a different bad experience
you have to check in the validate block
in fact all text columns should check for length.
All I do is use text which is like 4gb size on postgresql

–Users should know what does stop writing mean, but it could be nice to have some javascript working on character counting.

–add a beep if you break the limit
–beep alone is not useful,
need to give visual feedback

–js in the standard form?
–Yes, it's a feature you can turn on/off

–The JS is nice to have check before submit and then a validate error on backend. so if you don't have JS it tells you in the standard form error.

–The javascript could show something like "Max 100 chars, X remaining" while user types. that should be done in message key though problem is maintaining it. Or having some sort of extra help text ie: help_text + "acs-templaing.max_length_message".

–Informing before in the help text would be useful from the user perspective too

–Show a warning only when users reaches a threshold, i. ex. 90 chars "Ey! you write a lot! only X chars remaining". I've seen on, that you can write over the limit, then the counter become red.

–We can do all that, the idea is to make it a attribute of the form element

I have set a maxlength=200 on forum-new as big as database field length, but using spanish characters like 'tilde' (you know them as html entities: a acute, n tilde...) the html input text limit seems to exceed the database one.

I tried with that title: Foro con política cerrada, para probar. Además, pondré un título lo más largo posible para garantizar que está funcionando correctamente la última funcionalidad de limitar el tamaño máximo de los títu (that could be translated into something like "Closed policy forum, just for testing. I'm also writing a title as large as possible in order to test the new char limit feature on titles...").

I'm not sure what is happening, and how to fix that, but I think it's due to utf-8 translation into some other codification, i. ex. a pure ascii transliteration will carry a few extra characters: "Foro con pol'itica cerrada, para probar. Adem'as, pondr'e un t'itulo lo m'as largo posible para garantizar que est'a funcionando correctamente la 'ultima funcionalidad de limitar el tama~no m'aximo de los t'itu".

I think that issue should be taken into account.

PS: working with Oracle. I haven't tried that on PG yet.

I guess it depends how the database measures characters. Looks like the browser is counting UTF-8 characters while the database counts a character as one byte.

I suspect an easy solution is to increase the title to varchar(400) but set maxlength to 200.

It does show that automatically handling the maxlength and checking the size is more complex than we thought.

I wonder what [string length] would return on the utf-8 string. Learning this would help us understand if it counts bytes or characters in string length and if checking length in tcl before inserting would be any more effective than maxlength.

Hi guys,

with Oracle, there are a few approaches you can take. One is to change all your VARCHAR2 columns from BYTE to CHAR e.g. declare them as VARCHAR2(200 CHAR) instead of VARCHAR2(200). You would also need to do this with every VARCHAR2 variable in every PL/SQL procedure or function - a lot of work as you can imagine. However, it's worth knowing for a quick fix.

Oracle's recommended approach is to set NLS_LENGTH_SEMANTICS to 'CHAR', which basically tells Oracle to treat the length of every VARCHAR2 (and CHAR) column as being characters and not bytes.

However, the problem with both of these is that you'll still run into the maximum column size 4000 bytes. But now you won't be able to tell when you're going to hit it! I guess it depends on what languages you're using and how many double-byte characters they have. Fortunately for me, English has hardly any, but I did run into this problem once with an Irish-language client. I would say as a rule of thumb, if you think you're going to need more than 2000 characters, then why not use a CLOB?

See here for more detail
and a nice introduction
and a good example how-to here:

hope this helps


char in oracle are bytes, so yes, when an utf character uses 2 bytes it uses 2 "char" in oracle.

[string length] will return the number of character in the string according to the encoding and charset used. So even if a character uses 2 or 3 bytes and utf is the charset in use, string length will return 1 for that character. To get the number of bytes, [string bytelength] has to be used. I've used it a lot when working on oracle to check the string before an insert.

I like the idea of increasing the title length to 400 by default.

About setting maxlength to 200, could be done on ad_form?

I have made a function to know the database length of a field, called db_string_size In order to use that value on maxlength attributes, and also on -validate form params.

But I thought it was easier than what I now know, so maybe I should think a little more about that. Maybe, the best solution could be "always count utf strings as bytelength".

Just for documentation purposes, I've done that on forum new on forums/tcl/form-procs.tcl

set forum_name_max_size [ad_call_proc_if_exists db_column_size forums_forums name]
template::element create $form_name ${prefix}name \
-label [_ forums.Name] \
-datatype text \
-widget text \
-html "size 60 maxlength $forum_name_max_size" \
-validate {
blank {expr ![empty_string_p [string trim $value]]} { # forums.Forum_Name_can_not_be_blank # }
toolarge {expr [string bytelength $value] <= 200 } { # forums.name_max_chars # }

Note that catalog keys are spaced to be shown here instead of translated