仅在为Unity填写所有必填字段时才注册的条件语句

时间:2019-05-01 08:27:05

标签: c# unity3d conditional

我正在尝试创建一条if-else语句,在该语句中,即使所需的输入字段之一为空,也不会将任何信息插入数据库中

我尝试使用=等操作数!和==但无济于事,我似乎想不出另一种方法来获取我需要的条件语句。这是我尝试做的事情:

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 个答案:

答案 0 :(得分:1)

首先,您要检查InputField(不)是否等于'“”'。输入字段是一个对象,永远不会是字符串值。 您需要InputField.text

我还发现将条件分成单个语句并附加到错误字符串中很方便,这样调试器/用户就可以清楚地了解出了什么问题。然后,您还可以通过这种方式将错误发布到用户的对话框中。 请尝试以下操作:

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();
    }
}
相关问题