I turn to you again... I try to validate that the entered password has at least two (2) of the following special characters *?¡!#$%&
, I have already tried it with regex and it does not work for me, I tried it by going through the length of the password with a for
letter a letter and when I put the conditional to evaluate the letter, the application hangs until it stops on its own. Yes, it shows me the letter that it goes through, with a Toast
, but I don't know what happens that when comparing the character with a if
the application crashes and doesn't respond. This is one of the ways I've tried...
public void validarPassword(View v) {
String txtPassword = etPassword.getEditText().getText().toString();
if (txtPassword.isEmpty()) {
etPassword.setError("Campo obligatorio");
} else if (txtPassword.length() > 10) {
etPassword.setError("Demasiados caracteres");
} else {
boolean okpassword = false;
int contador = 0;
while (!okpassword) {
for(int i=0; i <txtPassword.length(); i++){
char posicion = txtPassword.charAt(i);
String letra = String.valueOf(posicion);
//Hasta aqui recorre letra a letra y me funciona si la muestro con un Toast, pero al poner el condicional ya la aplicación no funciona, se bloquea hasta que se detiene.
if (letra == "$" || letra == "?" || letra == "¡" || letra == "!") {
contador++;
}
}
if(contador <2 ){
okpassword = false;
}else {
okpassword = true;
}
}
Toast.makeText(getApplicationContext(), "Contraseña válida", Toast.LENGTH_LONG).show();
}
}
Another way I tried was to do it like this....
private boolean validarPassword() {
int caracteres = 0;
boolean okpassword= false;
String txtPassword = etPassword.getEditText().getText().toString();
if (txtPassword.isEmpty()) {
etPassword.setError("Campo obligatorio");
return false;
} else if (txtPassword.length() > 10) {
etPassword.setError("Demasiados caracteres");
return false;
} else {
for (int i = 0; i < txtPassword.length(); i++) {
String letra = String.valueOf(i);
//valido que el o los caracteres ingresados sean de acuerdo a los establecidos y no diferentes
if (!letra.matches("(?=.*[*?¡!#$%&])")) {
etPassword.setError("Sin caracter especial reconocido");
okpassword = false;
}else {
etPassword.setError(null);
caracteres ++;
}
if (caracteres < 2) {
etPassword.setError("Incluya al menos dos caracteres especiales");
okpassword = false;
}else {
okpassword = true;
break;
}
}
if(!okpassword == false){
etPassword.setError(null);
return true;
}else {
return false;
}
}
}
I appreciate if you can support me with your answers to know what I'm doing wrong, I'm just a student and I've done it based on forums I've found on the subject. Most forums talk is to validate that it has at least 1 special character, I need to validate that it has at least 2 special characters.
Well, I don't know why you use a
while
, I don't quite understand that part but what I did was remove it and modify yourelse
as follows:Now, you just have to adapt it to your needs because the code I made shows in it
Toast
each special character that was entered.