C# 只有在所有必填字段都为Unity填充时才注册的条件语句
我试图创建一个if-else语句,其中即使一个必需的inputfields为空,它也不会将任何信息插入数据库 我尝试过使用操作数,比如=!and==但是没有用,我似乎想不出另一种方法来获得我需要的条件语句。以下是我试图做的:C# 只有在所有必填字段都为Unity填充时才注册的条件语句,c#,unity3d,conditional,C#,Unity3d,Conditional,我试图创建一个if-else语句,其中即使一个必需的inputfields为空,它也不会将任何信息插入数据库 我尝试过使用操作数,比如=!and==但是没有用,我似乎想不出另一种方法来获得我需要的条件语句。以下是我试图做的: public InputField inputUserName; public InputField inputEmail; string CreateUserURL = "http://localhost/balikaral/insertAccount.php"
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");
}
}
首先,您要检查InputField notequals是否为“”。inputfield是一个对象,永远不会是stringvalue。 您需要InputField.text 此外,我发现将我的条件分离为单个语句并附加到errorstring中是一件很舒服的事情,这样调试器/用户就可以清楚地了解出了什么问题。然后,您还可以通过这种方式将错误发布到用户的对话框中。 请尝试以下操作:
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();
}
}