control maxlength property in input tag through struts validator

J

Jax

Hi,

Since the common-validator project provides a framework to configure
client and serverside validation I was looking how to control the
maxlength property of the input tags.

After much web-digging I couldn't come up with an answer. I bothers me
that one can define
<field property="firstname" depends="required,maxlength">
<var><var-name>maxlength</var-name><var-value>50</var-value></var>
and that this can result in proper client (javascript) and server-side
validation but that one still has to write:
<html:text property="firstname" MAXLENGTH="50"/>

It should be possile to enforce this through the validator framework,
right? My first idea was to send some javascript along (e.g.
document.theForm.firstname.maxlenght = 50) but that didn't work out
very well.

I'm hoping I overlooked some documentation and that something like
this is possible, because I really liked the idea of having all
validation rules in one place. And letting the user enter too much
characters only to refuse his input after the submission while it
could've been prevented earlier seems a waste of user's time.

Regards,
Olivier
 
J

Jax

I found one work-around by adding the following javascript to the
maxLength validator javascript:

var myFields = new Array();
var myMaxLength = new maxlength();
var theForm = document.forms[0];

for (x in myMaxLength) {
var myField = theForm[myMaxLength[x][0]];
if (myField.type == 'text' ||
myField.type == 'textarea') {
var iMax = parseInt(myMaxLength[x][2]("maxlength"));
myField.maxLength = iMax;
}
}

The only drawback is the forms[0] part. I have not figured out how to
get to the formname in the javascript section of the validator-rules.

Hope this helps someone, sometime....

Hi,

Since the common-validator project provides a framework to configure
client and serverside validation I was looking how to control the
maxlength property of the input tags.
[SNIP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top