Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validation ... best approach

 
Simon Joseph Aquilina
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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?

regards,
Sim085
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai kinera,

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

Thanks and Regards,
Sim085
 
Shamsher Dhaka
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Sim085
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic