Sorry for my bad English, I use Google translation.
I create an activity in which users must create a new profile. I place a restriction on editing text of 15 characters, and I want a warning if a new profile name has spaces or special characters. Like online video games
The following code helps me detect spaces, but not special characters.
I need help identifying special characters and displaying a warning in response.
@Override
public void onClick(View v) {
String nombre = nombreUsuario.getText().toString();
if (nombre.length() == 0) {
Toast aviso = Toast.makeText(getApplicationContext(), "Por favor introduce un nombre de Usuario", Toast.LENGTH_LONG);
aviso.show();
} else if (nombre.contains(" ") | nombre.contains("\\W")) {
Toast aviso = Toast.makeText(getApplicationContext(), "No son permitidos los espacios ni los caracteres especiales", Toast.LENGTH_LONG);
aviso.show();
} else {
nombre = nombreUsuario.getText().toString();
Plantilla entrada = new Plantilla(CrearUsuarioActivity.this);
entrada.abrir();
entrada.crearEntrada(nombre);
entrada.cerrar();
}
}
source
share