I have to validate a series of TextField in a JavaFX application. I think the best way would be regular expressions. But I'm not hitting the nail on the head. I need to validate an input that does not contain more than 25 characters and does not contain special characters like @#$€%€/()=? etc. My code is the following:
@FXML
void handleMarca(KeyEvent event) {
String m = txtMarca.getText()+event.getCharacter();
if( m.length() > 25 || m.matches("[^ A-Za-z0-9]" )){
ClaseAccesoria.dialogo("Escriba un nombre inferior a 25 caracteres.\n +
"Solo se admiten letras y dígitos.", ERROR);
}else{
Marca = txtMarca.getText();
}
}//fin metodo
As I have written the regular expression it would say that it puts "reject any character that is not A-Za-z0-9" But for now it accepts them without problem. Could someone tell me how to make a regular expression that does not accept special characters, or only accepts special characters and deny it. And if I can include that it only admits less than 25 characters....