开发者

JSF: Validation for Special characters

开发者 https://www.devze.com 2023-01-04 19:26 出处:网络
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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消