In this post I’m going to report a very nice function I implemented for limiting Form input fields, like textarea and input of type text. You have to call it via onkeyup and onkeydown events. The function accepts 3 parameters: field, limit and counterDesired. The first is the field object, you should use the keyword this for a value. The second parameter is a limit value, the number of characters that the field should contain at maximum. The third parameter is optional, false as default. It permits to add a visual counter after the field, like the one you see on YouTube, for example. You can either tell to the function to add the counter for you (put inside a __ block) or to put it inside another block you’ve already defined. In the second case, the block must have an id of the form ‘fieldID_counter’
This is the compact version, see below for some examples and the expanded, explained version:
This is the longer version for those who want a better understanding:
Because of Prototypejs, I’m sure this works with every browser, at least those supported by prototype, and I don’t have to worry about compatibility issues! I hope this may help you, and comment this post if you have suggestions or thoughts.
I do not use a commenting system anymore, but I would be glad to read your feedback. Feel free to contact me.