MohammedYakub M.
MohammedYakub M.

Reputation: 2941

JSF: Validation for Special characters

I want to apply validation for Special characters like (,/'#~@[]}{+_)(*&^%$£"!\|<>) using the jsf validator. i have implemented the validator for number but now i want to make validator for Special characters.

value.toString().trim().matches();

What will be the regular expression inside the matches() method in java class?

i want to allow all the field excluding the specified characters.

thanks in advance

Upvotes: 1

Views: 5372

Answers (1)

BalusC
BalusC

Reputation: 1108722

Why don't you just allow alphanumerical characters only?

if (!value.toString().trim().matches("\\p{Alnum}*")) {
    throw new ValidatorException(new FacesMessage("Invalid characters!"));
}

If you really want to go ahead in this direction. To check if the string contains those characters, use this:

if (value.toString().trim().matches(".*[,/'#~@\\x5B\\x5D}{+_)(*&^%$£\"!\\|<>]+.*")) {
    throw new ValidatorException(new FacesMessage("Invalid characters!"));
}

Keep in mind that there are much more characters available in the Unicode charset you'd probably also like to disallow.

Upvotes: 3

Related Questions