我正在创建一个具有登录/注册场景的Unity应用程序。 在注册场景中,我有标准字段,如姓名,姓氏,电子邮件,用户名,密码和密码。
我需要在注册用户之前进行错误检查,例如检查所有字段是否完整,电子邮件地址是否有效,或者密码是否匹配。如果其中一个或多个失败,则应显示错误消息。
当用户点击注册按钮时,下面是我的代码。 所有输入字段都在工作,我可以在脚本中输入和读取文本。我还可以在某些检查中显示错误消息。
我的问题是,当表单加载并且我点击注册而不填写任何字段时,我收到错误您必须填写所有字段。 如果我只填写第一个名称,然后单击注册,我的错误检查看起来像是通过所有字段必须填写测试并跳转到无效的电子邮件地址检查。我仍然应该所有字段必须填写错误。
如果表单已加载并且我只使用有效的电子邮件地址填写电子邮件字段,则会发生同样的情况。没有其他字段完成。验证会跳过所有其他检查,我可以注册。它似乎没有意识到所有其他字段都是空白的。
调试Register()函数显示所有输入文本长度实际上都是0,因此它不应该通过第一次检查。但确实如此。
在输入字段中我有占位符文本,但我不认为这是问题,因为字段上的调试显示加载时长度= 0。
// 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.");
}
}
答案 0 :(得分:3)
在第一个输入字段中,检查是否正在执行OR运算(||),因此如果任何一个条件为真,它将通过。你需要一切都是真的,因此使用AND(&&)而不是OR(||)。
如果你需要检查要填写的两个中的任何一个,那么将这两个特定的检查放在另外的括号中:
styles.less