Being able to limit the length of the content entered into a TextArea can be useful for various reasons. Although not supported natively by HTML, you can achieve the same effect using Javascript – on KeyUp and KeyDown events, check the length of the textarea value and truncate if necessary. We can also display how many characters are remaining if we want to provide user feedback.
Because I’ve used this technique in several Grails applications, I’ve created a simple TagLib – you may want to customize it to your requirements.
To use it:
- include the prototype library with <g:javascript library=”prototype”/>
- use <jttext:changeScript /> once on your page for the utility script
- define your textarea including the maxlength attribute with <jttext:textArea id=”messageField” maxlength=”20″ />
- use <jttext:statusDiv for=”messageField” /> to display the status message showing how many characters are left
So, a simple GSP would look like:
<html>
<head>
<g:javascript library=”prototype”/>
<jttext:changeScript />
</head>
<body>
<jttext:textArea name=”messageField” class=”sized” style=”width:320px;height:60px;” maxlength=”20″ />
<jttext:statusDiv for=”messageField” />
</body>
</html>
Effectively what you get looks like this (the HTML code has been slightly modified to work in this post):






{ 10 comments… read them below or add one }
Hi,
very useful. Would be worth packing into a plugin such that everybody can install it easily.
cheers
Dierk
Yes, I am hoping to produce a jtcommons plugin with some basic utilities that many applications could make use of – but I’m not sure when it will be ready so I thought I’d put this file out there now. Maybe I should just create grails-jtcommons-0.1 now, containing just this taglib?
Just noting a bug:
The textarea is not truncated when text is pasted using only the mouse.
Good stuff but do NOT copy and paste this code into NetBeans.
NetBeans translates the double quote marks into a different kind of double quote and the code does not work. You have to manually convert the strange looking double quotes into regular ones.
I’m getting a NPE trying to incorporate this into the Grails generated gsp.
Error executing tag : java.lang.NullPointerException: Cannot invoke method leftShift() on null object at C:/grails-app/views/civilProceedingsDescription/create.gsp:67
This is the tag entry in the gsp:
${fieldValue(bean:civilProceedingsDescriptionInstance,
field:’descriptionOfCase’)}
Any ideas?
Hmm, my code got whacked on my last comment … the gsp entry is:
[code]
${fieldValue(bean:civilProceedingsDescriptionInstance,field:'descriptionOfCase')} </jttext:textArea
[/code]
<jttext:textArea id=”descriptionOfCase” maxlength=”245″ name=”descriptionOfCase”> ${fieldValue(bean:civilProceedingsDescriptionInstance,
field:’descriptionOfCase’)} <⁄ jttext:textArea >
<jttext:statusDiv for=”descriptionOfCase” ⁄ >
Thanks Spark, I’ve reproduced the error using the latest grails (1.2.2) – it seems the outputAttributes() method is not available or has changed since I wrote this – I probably wrote it using grails-1.0.4 – quite some time ago.
I’ll have a look around for a solution…
I’ve fixed the grails-jttaglib plugin but haven’t been able to release it yet because I’ve got subversion authentication problems.
Hopefully I’ll get that resolved soon, and you’ll be able to use grails-jttaglib-0.3 with grails-1.2.2 and see this working.
Thanks Paul.
I had to upgrade to Grails 1.2.2, I was using 1.2.1. But once I updated Grails, and installed the latest jttaglib, I got it working.
{ 1 trackback }