heyred
heyred

Reputation: 2051

Unity registration form validation

I am making a Unity app that has a login/register scene. On the registration scene, I have standard fields like first name, last name, email, username, password, and password again.

I need to do error checks before registering the user, such as checking if all fields are complete, if the email address is valid, or if the passwords match. If one or more of these fails, it's supposed to show an error message.

Below is my code when the user clicks the Register button. All input fields are working, and I can input and read text from them in my script. I can also show the error messages on some checks.

My problem is that when the form loads and I click register without filling in any fields, I get the error saying you have to complete all fields. If I fill in just the first name then and click register, my error check looks like it passes the all fields must be filled in test and jumps to the invalid email address check. I should still be getting the all fields must be filled in error.

The same thing happens if the form is loaded and I just fill in the email field with a valid email address. No other fields completed. The validation skips all other checks and I can register. It does not seem to recognize that all the other fields are blank.

Debugging the Register () function shows that all input text lengths are in fact 0, so it should not pass the first check. But it does.

On the input fields I have placeholder text but I dont think that is the issue as the Debug on the fields show they are Length = 0 on load.

// Register button clicked
public void Register ()
{
    Debug.Log("Input field lengths (returns correct lengths depending on input fields completed)" +
        "\nFirst Name Length: " + firstNameInputField.text.Length +
        "\nLast Name Length: " + lastNameInputField.text.Length +
        "\nEmail Length: " + emailInputField.text.Length +
        "\nUsername Length: " + usernameInputField.text.Length +
        "\nPassword Length: " + passwordInputField.text.Length +
        "\nPassword again Length: " + passwordAgainInputField.text.Length);

    // Input fields check
    if (firstNameInputField.text.Length     != 0 ||
        lastNameInputField.text.Length      != 0 ||
        emailInputField.text.Length         != 0 ||
        usernameInputField.text.Length      != 0 ||
        passwordInputField.text.Length      != 0 ||
        passwordAgainInputField.text.Length != 0)
    {
        // Success - All input fields completed
        // Check if password/password agian match
        if (string.Compare(passwordInputField.text, passwordAgainInputField.text) == 0)
        {
            // Success - Passwords match
            // Validate email
            if (ValidateEmail(emailInputField.text))
            {
                // Success - Email valid
                // POST details to database
                StartCoroutine(RegisterUser());
            }
            else
            {
                // Error - Email not valid
                ShowErrorMessage("ALERT!" +
                    "\n\nYour email is invalid." + 
                    "\nPlease check the spelling and try again.");
            }
        }
        else
        {
            // Error - Passwords dont match
            ShowErrorMessage("Your passwords do not match." +
                "\n\nPlease check the spelling and try again." +
                "\nNote that passwords are case-sensitive.");
        }
    }
    else
    {
        // Error - Empty input fields
        ShowErrorMessage("ALERT!" +
            "\n\nYou must complete all sections.");
    }
}

Upvotes: 3

Views: 3673

Answers (1)

Raviraj Palvankar
Raviraj Palvankar

Reputation: 879

In the first input fields checks you are doing a OR operation (||) and hence it will pass if any one of the condition is true. You need all to be true hence use AND (&&) instead of OR (||).

If you need a check for either of two to be filled then put those two specific checks in additional parenthesis:

if (condition1 && (condition1 || condition2))

Upvotes: 3

Related Questions