views:

33

answers:

1

hi,all

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

+1  A: 

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.

BalusC
Hi,Balus thanx 4 reply...but i want to restrict only the listing characters...all others will be allowed...(i.e. "." and white spaces ,etc.. are allowed)
yakub_moriss
How about the million other characters in [Unicode](http://www.fileformat.info/info/unicode/block/index.htm)? You'd rather like to match on **allowed** chars, not on *disallowed* chars.
BalusC
you r right...but its not solve my problem...
yakub_moriss
OK, I added a 2nd example
BalusC
Thank you very much....Balus
yakub_moriss
Hi Balus...can we display this message from message.property file ?
yakub_moriss