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
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.
精彩评论