Ad

Struts2 Annotation-Validators For Invalid Chars

While using Struts2, I am using several annotations to do my validations inside the Model class, in the set() methods, like:

@RequiredStringValidator(message = "Name is required")
@StringLengthFieldValidator(message = "Name must be between 5 and 60 characters", 
                              minLength = "5", maxLength = "60")
public void setName(String name) {
    this.name = name;
}

But I would like how to put a validation do avoid Strange characters, like !, ?, #, $ and etc...

Is there any existing validator I should use like a @RestrictChars or maybe is there some way to use the @StringRegexValidator. Or should I write a new one. Any hint or documentation how to do such would be great.

Ad

Answer

I found it finally:

Just adding the already existent @RegexFieldValidator gave me the Validator I needed:

@RegexFieldValidator(message = "Use only Letters or numbers",
                       expression = "^[a-zA-Z0-9]+$") 

Thanks anyway guys!

Ad
source: stackoverflow.com
Ad