Answer the question
In order to leave comments, you need to log in
How do you like the option, in terms of reliability and correctness?
I write like this in a servlet
//Проверка на валидность лицензиада
if (!checkLices(lices)) {
LOG.error("Запрещенные символы в " + lices);
out.print("<h1>Извините, произошла ошибка в строке: " + lices + "</h1><br>"
+ "разрешены только: латиница, цифры, (), -, _, пробел");
return;
}
//Проверка на валидность otpCount, clientsCount, certPeriod
if (!checkNumbers(otpCount) || !checkNumbers(clientsCount) || !checkNumbers(certPeriod)) {
LOG.error("Запрещенные символы в одной из строк: " + otpCount + " " + clientsCount + " " + certPeriod);
out.print("<h1>Ошибка, В 1, 2, 5 полях разрешены только цифры!</h1>");
return;
}
private boolean checkLices(String checkString) {
Pattern p = Pattern.compile("[A-Za-z0-9-_ ()]+");
Matcher m = p.matcher(checkString);
return m.matches();
}
private boolean checkNumbers(String checkNum) {
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(checkNum);
return m.matches();
}
Answer the question
In order to leave comments, you need to log in
Without particularly delving into the logic, we can immediately say that Pattern instances must be at least static.
for example
...
private static final Pattern NUMBER = Pattern.compile("[0-9]+");
...
private boolean isNumber(String src) {
return NUMBER.matcher(src).matches();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question