我正在制作一個(gè)具有登錄/注冊場景的 Unity 應(yīng)用程序。在注冊場景中,我有標(biāo)準(zhǔn)字段,如名字、姓氏、電子郵件、用戶名、密碼和密碼。我需要在注冊用戶之前進(jìn)行錯(cuò)誤檢查,例如檢查所有字段是否完整、電子郵件地址是否有效或密碼是否匹配。如果其中一個(gè)或多個(gè)失敗,它應(yīng)該顯示錯(cuò)誤消息。以下是用戶單擊“注冊”按鈕時(shí)的代碼。所有輸入字段都在工作,我可以在我的腳本中輸入和讀取它們中的文本。我還可以在某些檢查中顯示錯(cuò)誤消息。我的問題是,當(dāng)表單加載并單擊注冊而不填寫任何字段時(shí),我收到錯(cuò)誤消息,提示您必須完成所有字段。 如果我只填寫名字然后單擊注冊,我的錯(cuò)誤檢查看起來好像通過了所有字段必須填寫的測試并跳轉(zhuǎn)到無效的電子郵件地址檢查。我仍然應(yīng)該得到所有字段必須填寫錯(cuò)誤。如果表單已加載并且我只是使用有效的電子郵件地址填寫電子郵件字段,則會(huì)發(fā)生同樣的事情。沒有完成其他字段。驗(yàn)證跳過所有其他檢查,我可以注冊。它似乎沒有認(rèn)識(shí)到所有其他字段都是空白的。調(diào)試Register()函數(shù)顯示所有輸入的文本長度實(shí)際上都是0,所以應(yīng)該不會(huì)通過第一次檢查。但確實(shí)如此。在輸入字段上,我有占位符文本,但我認(rèn)為這不是問題,因?yàn)樽侄紊系恼{(diào)試顯示它們在加載時(shí) Length = 0。// Register button clickedpublic 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()); }
1 回答

不負(fù)相思意
TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
在第一個(gè)輸入字段中檢查您正在執(zhí)行 OR 操作 (||),因此如果任何一個(gè)條件為真,它將通過。您需要全部為真,因此使用 AND (&&) 而不是 OR (||)。
如果您需要對(duì)兩項(xiàng)中的任何一項(xiàng)進(jìn)行檢查,請(qǐng)將這兩項(xiàng)特定檢查放在附加括號(hào)中:
if (condition1 && (condition1 || condition2))
- 1 回答
- 0 關(guān)注
- 217 瀏覽
添加回答
舉報(bào)
0/150
提交
取消