File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes whitespace validation?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "whitespace validation??" Watch "whitespace validation??" New topic

whitespace validation??

Sumithab Baskaran
Ranch Hand

Joined: Oct 29, 2004
Posts: 52
Hi All!

I am validating a text field which is an integer.

Here is the code:
<field property="dataFactoryFileId" depends="maxlength,integer">
<msg name="integer" key="fileSearchForm.dataFactoryFileId.integer" />
<arg0 key="fileSearchForm.dataFactoryFileId.displayname" /> <arg1 name="maxlength" key="${var:maxlength}" resource="false"/>

When the user enters an integer like this: 2 0 9 6 9 5
Right now an error message is displayed: the field must be an integer.

How do I validate this and display an error message like - "do not use spaces while entering an integer"

Thanks in advance!
Vani Bandargal
Ranch Hand

Joined: Oct 06, 2005
Posts: 82
you need to write a custom validation using Regular expressions
Steve McCann
Ranch Hand

Joined: Oct 20, 2004
Posts: 81
Try swapping the two validations around so that it checks for a valid integer first. I have not tried it but I assume 2 3 4 is not a valid integer.

alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
no custom validation necessary. use the 'mask' rule and check using this regex: ^[0-9]*$ .

-/a<br />certified slacker...yes, my last name is 'do' - <a href="" target="_blank" rel="nofollow"></a>
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
oh, and the mask rule's error message can be customized to say whatever you want.
Marc Peabody
pie sneak

Joined: Feb 05, 2003
Posts: 4727

To avoid the extra spaces in submissions I use a Filter and RequestWrapper combination to trim all request parameter values. A submission of " " would get trimmed to "" and "88 " would get trimmed to "88". It really does the trick.

Note that my solution only takes care of leading and trailing spaces but the idea might help you to come up with a different kind of solution to the problem.
[ October 25, 2005: Message edited by: Marc Peabody ]

A good workman is known by his tools.
I agree. Here's the link:
subject: whitespace validation??
It's not a secret anymore!