Home > Java, Web > Limiting the content of a TextArea

Limiting the content of a TextArea

by paul on March 6, 2008

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):

 

Download TagLib here.

{ 10 comments… read them below or add one }

Dierk König March 6, 2008 at 8:14 am

Hi,
very useful. Would be worth packing into a plugin such that everybody can install it easily.

cheers
Dierk

paul March 6, 2008 at 6:09 pm

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?

Nate February 19, 2009 at 12:29 pm

Just noting a bug:

The textarea is not truncated when text is pasted using only the mouse.

John March 8, 2009 at 8:42 am

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.

Spark April 14, 2010 at 4:41 am

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?

Spark April 14, 2010 at 4:57 am

Hmm, my code got whacked on my last comment … the gsp entry is:
[code]
${fieldValue(bean:civilProceedingsDescriptionInstance,field:'descriptionOfCase')} </jttext:textArea

[/code]

Spark April 14, 2010 at 5:10 am

<jttext:textArea id=”descriptionOfCase” maxlength=”245″ name=”descriptionOfCase”> ${fieldValue(bean:civilProceedingsDescriptionInstance,
field:’descriptionOfCase’)} <⁄ jttext:textArea >
<jttext:statusDiv for=”descriptionOfCase” ⁄ >

paul April 19, 2010 at 10:58 am

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…

paul April 19, 2010 at 6:22 pm

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.

Spark May 11, 2010 at 7:40 am

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.

Leave a Comment

{ 1 trackback }

Previous post:

Next post: