Advertisement
tiagooleite2

Untitled

Nov 15th, 2020
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package expressaoregular;
  2.  
  3. public class MascaraValidacaoNome {
  4.    
  5.     private static final String REGEX_ESPACO = "\\s";
  6.    
  7.     public static boolean ehNomeValido(String nome) {
  8.         if (nomeValido(nome) && nomeNaoContemCharEspecial(nome)
  9.                 && nomeSobrenomeIniciaMuisculo(nome)
  10.                 && letrasDoNomeEhMinusculo(nome)) {
  11.             return true;
  12.         }
  13.         return false;
  14.     }
  15.  
  16.     private static boolean letrasDoNomeEhMinusculo(String nome) {
  17.         String[] nomeQuebrado = nome.split(REGEX_ESPACO);
  18.         for (String itemNome : nomeQuebrado) {
  19.             for (int i = 1; i < itemNome.length(); i++) {
  20.                 if (Character.isUpperCase(itemNome.charAt(i))) {
  21.                     return false;
  22.                 }
  23.             }
  24.         }
  25.         return true;
  26.     }
  27.  
  28.     private static boolean nomeSobrenomeIniciaMuisculo(String nome) {
  29.         String[] nomeQuebrado = nome.split(REGEX_ESPACO);
  30.        
  31.         String primeiroNome = nomeQuebrado[0];
  32.         String segundoNome = nomeQuebrado[1];
  33.        
  34.         if (Character.isUpperCase(primeiroNome.charAt(0))
  35.                 && Character.isUpperCase(segundoNome.charAt(0))) {
  36.             return true;
  37.         }
  38.         return false;
  39.     }
  40.  
  41.     private static boolean nomeNaoContemCharEspecial(String nome) {
  42.         String[] nomeQuebrado = nome.replace(" ", "").split("");
  43.         for (String character : nomeQuebrado) {
  44.             boolean matches = character.matches("\\p{Alpha}");
  45.             if (!matches) {
  46.                 return false;
  47.             }
  48.         }
  49.         return true;
  50.     }
  51.  
  52.     private static boolean nomeValido(String nome) {
  53.         if (nome == null || nome.trim().isEmpty()) {
  54.             return false;
  55.         }
  56.        
  57.         try {
  58.             String[] nomeQuebrado = nome.split(REGEX_ESPACO);
  59.             if (nomeQuebrado.length > 2 || nomeQuebrado.length < 2) {
  60.                 return false;
  61.             }
  62.            
  63.         } catch (Exception e) {
  64.             e.printStackTrace();
  65.             return false;
  66.         }
  67.        
  68.         return true;
  69.     }
  70.  
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement