Reputation: 3399
I am creating and Activity and I need to validate the correct input type in a Text Area. I want to avoid any XSS or Mysql Injection.
I want to accept [a-z] [A-Z] [0-9] [.] [,] and whitespace.
The only thing that I got by now is:
Pattern pattern_ = Pattern.compile("^[:alpha:]+(?:(?:\\s+|-)[:alpha:]+)*$");
How would you implement that?
Upvotes: 0
Views: 880
Reputation: 26370
Why don't just do this:
Pattern pattern_ = Pattern.compile("^[a-zA-Z0-9\.,\s]+$");
Upvotes: 1
Reputation: 12486
If you only want to accept exactly the characters [a-z]
, [A-Z]
, [0-9]
, .
, and ,
, the most direct way of doing that is to match the regular expression ^[a-zA-Z0-9.,\s]*$
. (See this on Regexr.)
Depending on the data, severely restricting the input like this is not a good idea.
If your data entry field is for people's names, it won't allow people to enter names with accented characters, or names in languages other than English. Thévenin
(a French name) would be out because it contains an accented é
. The Chinese family name 伟
would also be illegal. The Greek name Αφρουδάκης
would be entirely illegal.
Your language should have ways of sanitizing input data. These will prevent SQL or XSS attacks without prohibiting otherwise harmless data like 伟
. Refer to this question for a general overview of input sanitization: What are the best PHP input sanitizing functions?
Upvotes: 1