R
R
random2014-11-29 14:44:27
Java
random, 2014-11-29 14:44:27

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();
    }


And I also wanted to ask how to allow the input of a quad in the pattern. brackets([])?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YV, 2014-11-30
@demon123

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]+");
...

the names for the methods are also not chosen in the best way, because the "check" usually checks "something" and if "something" is invalid, then this method should trigger the exception.
In your case, it is better to use the method from the prefix "is"
private boolean isNumber(String src) {
    return NUMBER.matcher(src).matches();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question