There is a user login, and the requirements are the following:
- The login must start with a Latin letter.
- The login must finish with either a Latin letter or a digit.
- There may also be digits, dots, and minus signs in the login.
- Min. login length is 1.
- Max. login length is 20.
Here is my Java code.
boolean checkLogin(String login) {
final int maxLength = 20;
if (login == null || login.isEmpty() || login.length() > maxLength) {
return false;
}
final Pattern pattern = Pattern.compile("^[a-z][a-z\\d\\.\\-]*[a-z\\d]+$", Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(login);
return matcher.matches();
}
Is everything OK?