I am trying to create an if-else statement where if even one of the required inputfields is empty, it will not insert any of the information into the database

i've tried using operands such as =! and == but to no avail, i can't seem to think of another way to get the conditional statement i need. Here is what I have tried to do:

public InputField inputUserName;
public InputField inputEmail;

    string CreateUserURL = "http://localhost/balikaral/insertAccount.php";

    public void verif()
    {
        if (inputUserName != "" && inputEmail != "")
        {
            CreateUser(); //method which contains the function to insert the inputted data into the database
        }
        else
        {
            print("error");
        }
    }

1 Answers

1
Immorality On Best Solutions

First of all, you are checking if the InputField (not)equals '""'. The inputfield is an object and will never be a stringvalue. You want InputField.text.

Also I find it comfortable to seperate my conditions into single statements and append to an errorstring, so that the debugger/ the user has a clear understanding of what goes wrong. You could then also post the errors into a dialogue box for your user this way. Try the following:

public void verif()
{
    StringBuilder errorBuilder = new StringBuilder();

    if (string.IsNullOrWhiteSpace(inputUserName.text))
    {
        errorBuilder.AppendLine("UserName cannot be empty!");
    }


    if (string.IsNullOrWhiteSpace(inputEmail.text))
    {
        errorBuilder.AppendLine("Email cannot be empty!");
    }

    // Add some more validation if you want, for instance you could also add name length or validate if the email is in correct format

    if (errorBuilder.Length > 0)
    {
        print(errorBuilder.ToString());
        return;
    }
    else // no errors
    {
        CreateUser();
    }
}