Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package expressaoregular;
- public class MascaraValidacaoNome {
- private static final String REGEX_ESPACO = "\\s";
- public static boolean ehNomeValido(String nome) {
- if (nomeValido(nome) && nomeNaoContemCharEspecial(nome)
- && nomeSobrenomeIniciaMuisculo(nome)
- && letrasDoNomeEhMinusculo(nome)) {
- return true;
- }
- return false;
- }
- private static boolean letrasDoNomeEhMinusculo(String nome) {
- String[] nomeQuebrado = nome.split(REGEX_ESPACO);
- for (String itemNome : nomeQuebrado) {
- for (int i = 1; i < itemNome.length(); i++) {
- if (Character.isUpperCase(itemNome.charAt(i))) {
- return false;
- }
- }
- }
- return true;
- }
- private static boolean nomeSobrenomeIniciaMuisculo(String nome) {
- String[] nomeQuebrado = nome.split(REGEX_ESPACO);
- String primeiroNome = nomeQuebrado[0];
- String segundoNome = nomeQuebrado[1];
- if (Character.isUpperCase(primeiroNome.charAt(0))
- && Character.isUpperCase(segundoNome.charAt(0))) {
- return true;
- }
- return false;
- }
- private static boolean nomeNaoContemCharEspecial(String nome) {
- String[] nomeQuebrado = nome.replace(" ", "").split("");
- for (String character : nomeQuebrado) {
- boolean matches = character.matches("\\p{Alpha}");
- if (!matches) {
- return false;
- }
- }
- return true;
- }
- private static boolean nomeValido(String nome) {
- if (nome == null || nome.trim().isEmpty()) {
- return false;
- }
- try {
- String[] nomeQuebrado = nome.split(REGEX_ESPACO);
- if (nomeQuebrado.length > 2 || nomeQuebrado.length < 2) {
- return false;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement