File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Validation ... best approach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validation ... best approach" Watch "Validation ... best approach" New topic

Validation ... best approach

Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102

Basically I am already using validation in my web application to limit an entry to only numbers, and to set maximum size of a text field by using the validation.xml file.

However I now want to make validation so that only a characters that I specify would be accepted. I want that only the following characters are excepted:

a-z, A-Z, 0-9, ', @, (, ), &, $, �, %, !, :, ;, ., ,(comma), /, \, ?, {, }

My problem is that some users are inserting bullet characters in my database, and thus freeking out the output, and thus I want to restrick the character input to the characters I typed above.

What is the best aprach to do this in struts?

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you're using the Struts Validation framework, you'd want to use the built-in validation rule of "mask", in which you validate the field against a regular expression (regex).

I'm afraid I'm no good with regex, so I can't help you with figuring out what the correct regex expression would be, but do some research, and I'm sure you'll come up with the right expression.

Consultant, Sima Solutions
sai kinnera
Ranch Hand

Joined: Aug 12, 2004
Posts: 52
You can try this regular expression if you are using jdk1.4 and above:

Pattern p1=Pattern.compile("[a-zA-Z0-9'@()&$%!,:;./\?{}]{1,maxSize}");

{1,maxSize}-- means the variable should be at least a char length and can have a length of whatever value you give for maxSize.
Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
sai kinera,

where would I put that pattern.compile thing? In the action class?

Thanks and Regards,
Shamsher Dhaka

Joined: Aug 30, 2006
Posts: 1
You need to create a Validation Rules class and compile your regex pattern using jdk 1.4 class like
Pattern mask = Pattern.compile(ZIP_REGEX);
Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
Ok, I understand.

So I create another class that would return tru or false according to whether the string I pass as an argument is valid or not! am I right?

also, is this something I can bind with Struts Validation (like telling Struts to use that class the validate the TextFields).

I am sorry if I am not understanding completly, I am quite new to Struts.

thanks and Regards,
I agree. Here's the link:
subject: Validation ... best approach
It's not a secret anymore!