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

Struts - Validation

Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
I would like to use some of the validations that the validator framework comes with, but all of my values have commas in them. How would I strip out the comma values and then use a validation such as floatRange. Ideally I would like to be able to validate all cash quantities. 10,000 as well as 10.58 as valid.

Your help is appreciated, thanks,

N.D:"Anything worth having, takes time to get"
Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
I just dealt with something like that this morning actually.

Unfortunetly I don't think you can use the in-built validation on a numeric field with commas in it, as it will always seems unacceptable.

I ended up having to do build a small method to strip out all extra characters (leaving only numbers and the decimal point) and doing manual validation against that value.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
You could probably use the mask built-in validation.
Another option I can think of is to create your own validation and register it with validation.xml, but unfortunately the Struts Validator guide doesn't seem to say much about how to do that.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Marc Peabody
pie sneak

Joined: Feb 05, 2003
Posts: 4727

You're pretty much going to having to write your own validation.

It's not that hard. Just create a new class/method that you point to with the validator-rules.xml. If you have problems with it or face a lack direction just post concerns.

If you wish to reuse some of the framework's functionality, you can use the GenericValidator from your customized method to handle range and float validation.

A good workman is known by his tools.
Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
Hello Everyone,

I have decided to format the string using onblur event with javascript on the front end. However with the javascript function my value will still have commas in it. Is it possible for a function to strip out the commas and hand over the value to the rest of the validations.

I'm thinking I have to create my own validation method that really doesnt validate but formats the value, and then follow it up with dependant validations.

<form name="someFormWithMoneyFields">
<field property="cashAmount" depends="formatMoney, floatRange, required"/>

where "formatMoney" would really be my format method?

Would this work? is it good practice?

-Thanks for any help
I agree. Here's the link:
subject: Struts - Validation
It's not a secret anymore!