GeeCON Prague 2014*
The moose likes Struts and the fly likes Struts2 Annotations validate String using RegexFieldValidator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 Annotations validate String using RegexFieldValidator" Watch "Struts2 Annotations validate String using RegexFieldValidator" New topic
Author

Struts2 Annotations validate String using RegexFieldValidator

Alvaro Lopez
Greenhorn

Joined: Apr 05, 2013
Posts: 4
I am trying to use Struts2 Annotations to validate some fields of a bean.
In this case, I would like to validate a phone number String with a regex.

Googling aroung I found the @RegexFieldValidator annotation, so I included in my class and made some tests:



However, any regex expression seemed to work. I tried even with "\\d{9}" and "[0-9]{9}".
Even with type=ValidatorType.SIMPLE and:


The Required StringValidator works properly...What´s going on here?
Is it possible to use this validator properly?

Thanks in advance
Yogesh Lonkar
Ranch Hand

Joined: Jul 17, 2012
Posts: 94

Try using this expression="\\d{3}-\\d{3}-\\d{4}"


Learning some thing New Every Day
Alvaro Lopez
Greenhorn

Joined: Apr 05, 2013
Posts: 4
It still accepts any value. It seems to be a problem with @RegexFieldValidator... :S

I used:

In the AccountAction, I introduced some Validations:



And the account phone is being set in a JSP through:

Yogesh Lonkar
Ranch Hand

Joined: Jul 17, 2012
Posts: 94

Can you please provide your action class code? because @RegexFieldValidator(message="Invalid Phone Number",expression="\\d{3}-\\d{3}-\\d{4}") works perfectly.
Alvaro Lopez
Greenhorn

Joined: Apr 05, 2013
Posts: 4
I´m sorry but it doesn´t work. I tried directly with a field in the Action class, not in the Account Bean class and it accepts any string as phone2 value

Let´s see the case, just with the phone2 field In NewAccount.jsp I have:



In AccountActionClass.java:



In the JSP I just fill the field phone2 with any string (figures or letters). While the other fields show an error message as they are null, the phone2 label is black and it seems to be correct.

So..what am I doing wrong?

Thanks in advance.
Yogesh Lonkar
Ranch Hand

Joined: Jul 17, 2012
Posts: 94

OK here is how i do it.
Model Class


Action class

and this should do it for @VisitorFieldValidator link to Docs
Alvaro Lopez
Greenhorn

Joined: Apr 05, 2013
Posts: 4
I am using Struts 2.3.12.

Maybe is it compulsory to use javax.persistence package in order @VisitorFieldValidator to work? I bet it isn't.

I have tried some more tests but @RegexFieldValidator doesn't work properly .
It sets the String (in account.phone or in phone2) but it doesn't use the Regex to validate it, so this annotation is not working for me.

However, @RequiredStringValidator works perfectly for both fields. If I don't write anything, the error message appears!

I suppose I have to validate the phone number in another way...maybe with @IntRangeFieldValidator

Thanks for your kind help.
 
GeeCON Prague 2014
 
subject: Struts2 Annotations validate String using RegexFieldValidator