vendredi 27 novembre 2020

Error message displayed even if entered value is valid in Java

I have this method to validate an email entered by user.

Now the method is printing the error even if the email is blank.

I don't want that behavior. I just want the error message to be printed when the email is not alpha because the empty email is accepted has a valid value.

 public static String validateEmail() {
            String email;
            boolean isAlpha;
            boolean isValid = false;
    
                do {
                    email = validateString(MSG_SOLL_EMAIL, MSG_ERR_EMAIL, 5, 50,
                            false);
    
                        isAlpha = Tp2Utils.isAlphaNumPlus(email, "@,.,_")
                                && Tp2Utils.hasNCar(email, '.') >= 1
                                && Tp2Utils.hasNCar(email, '@') == 1;
                       
                       if (email.isBlank()) {
                           isValid = true;
                       }
                        if (!email.isBlank() && isAlpha) {
                            isValid = true;
                        } else {
                            System.out.println(MSG_ERR_EMAIL);
                        }
    
                } while (!isValid && !isAlpha);
    
            return email.trim();
        }

I would appreciate some help to make my method work as I want it to.

Thank's

Aucun commentaire:

Enregistrer un commentaire