package at.gv.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.Period; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.regex.Pattern; public class ValidationUtils { private static final String DATEFORMAT = "dd.MM.yyyy"; private static final int REPRESENTATION_MIN_AGE = 14; public static boolean isDateFormat(String dateString) { if (dateString.length() > DATEFORMAT.length()) return false; SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT); try { sdf.parse(dateString); return true; } catch (ParseException e) { return false; } } public static boolean isEmailAddressFormat(String address) { if (address == null) { return false; } return Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$").matcher(address).matches(); } public static boolean containsPotentialCSSCharacter(String param) { if (param == null) { return false; } return param.indexOf(";") != -1 || param.indexOf("%") != -1 || param.indexOf("\"") != -1 || param.indexOf("'") != -1 || param.indexOf("ยด") != -1 || param.indexOf("`") != -1 || param.indexOf(",") != -1 || param.indexOf("<") != -1 || param.indexOf(">") != -1 || param.indexOf("\\") != -1 || param.indexOf("#") != -1 || param.indexOf("/") != -1; } public static boolean isValidPostCode(String postcode) { if (postcode == null) return false; return Pattern.compile("[0-9]{4}").matcher(postcode).matches(); } public static boolean isOverThirteen(String representativeDateOfBirth) { try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATEFORMAT); LocalDate birthdate= LocalDate.parse(representativeDateOfBirth, formatter); Period p = Period.between(birthdate, LocalDate.now()); return p.getYears() >= REPRESENTATION_MIN_AGE; } catch (DateTimeParseException e) { return false; } } }