仅在为 Unity 填写所有必填字段时才注册的条件语句
Conditional statements that only registers if all required fields are filled for Unity
我正在尝试创建一个 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");
}
}
首先,您要检查 InputField
(不)是否等于“””。 inputfield 是一个对象,永远不会是一个字符串值。
你想要 InputField.text
.
我还发现将我的条件分成单个语句并附加到错误字符串很舒服,这样调试器/用户就可以清楚地了解出了什么问题。然后,您还可以通过这种方式 post 将错误输入到用户的对话框中。
请尝试以下操作:
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();
}
}
我正在尝试创建一个 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");
}
}
首先,您要检查 InputField
(不)是否等于“””。 inputfield 是一个对象,永远不会是一个字符串值。
你想要 InputField.text
.
我还发现将我的条件分成单个语句并附加到错误字符串很舒服,这样调试器/用户就可以清楚地了解出了什么问题。然后,您还可以通过这种方式 post 将错误输入到用户的对话框中。 请尝试以下操作:
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();
}
}