Problem when validating that only fields with letters exist using regex

0

I have a method that validates the input formats and inside I have a regular expression that only accepts letters from the a-z. The problem is that I try it with only letters but it does not work, it returns false in the first if. I leave the code below:

public boolean formato() {
    Pattern p = Pattern.compile("[a-z].*");
    Pattern numero = Pattern.compile("[0-9].*");
    //solo campos con letra
    Matcher nombre = p.matcher(txtrut.getText());
    Matcher apellido_m = p.matcher(txtapellido_m.getText());
    Matcher apellido_p = p.matcher(txtapellido_p.getText());
    Matcher sector = p.matcher(txtsector.getText());

    //solo campos numericos
    Matcher t_zapato=numero.matcher(txtzapato.getText());
    Matcher t_pantalon=numero.matcher(txtpantalon.getText()); 
    Matcher t_chaqueta=numero.matcher(txtchaqueta.getText());
    Matcher fono=numero.matcher(txtfono.getText());
    Matcher nro=numero.matcher(txtnro_casa.getText());

    //aqui en este mensaje devuelve false 
    System.out.println("el nombre es: "+nombre.matches());

    if(nombre.matches()==false) { System.out.println(" nombre incorrecto"); return false; }
    else if (apellido_m.matches()==false) { System.out.println("apellido_m incorrecto"); return false; }
    else if (apellido_p.matches()==false) { System.out.println("apellido_p incorrecto");return false; }
    else if(sector.matches()==false) { System.out.println("sector incorrecto");return false;}
    else if(t_zapato.matches()==false) { System.out.println("zapato incorrecto"); return false;}
    else if (t_pantalon.matches()==false) { System.out.println("pantalon incorrecto");  return false;}
    else if (t_chaqueta.matches()==false) { System.out.println("chaqueta incorrecta");return false;}
    else if( fono.matches()==false) { System.out.println("fono incorrecta");return false;}
    else if(nro.matches()==false) { System.out.println("nro de casa incorrecto");return false;}
    return true;
}
    
asked by jose miguel jara 08.02.2017 в 20:14
source

1 answer

2

The problem is that you are testing a rut field that will probably contain numbers check your code

In addition, as additional information, I can tell you that

RegExp:

[a-z] <= Solo Minúsculas
[A-Z] <= Solo Mayusculas
[a-zA-Z] <= Minusculas y Mayusculas
.* <= Cualquier Cosa

If you only want to accept letters you have to use.

[a-zA-Z]+ <= 1 o mas letras.
[a-zA-Z]* <= 0 o mas letras.

The same with the Numbers.

[0-9]+ <= 1 o mas Números.
[0-9]* <= 0 o mas Números.
    
answered by 08.02.2017 / 20:42
source