From d65dd808377d4e52c828f1c04b516671383482fe Mon Sep 17 00:00:00 2001 From: huyulong <1838407198@qq.com> Date: Thu, 2 Jan 2025 17:02:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/Script/loginScripts/LoginPanel.cs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/xiaofang/Assets/Script/loginScripts/LoginPanel.cs b/xiaofang/Assets/Script/loginScripts/LoginPanel.cs index 386e45d2..76387db1 100644 --- a/xiaofang/Assets/Script/loginScripts/LoginPanel.cs +++ b/xiaofang/Assets/Script/loginScripts/LoginPanel.cs @@ -34,11 +34,53 @@ public class LoginPanel : Base { Debug.Log("没有找到上次登录的账号."); } + + // 初始化:监听输入框和按钮事件 + id.onValueChanged.AddListener(OnAccountInputChanged); + pwd.onValueChanged.AddListener(OnPasswordInputChanged); loginBtn.onClick.AddListener(OnClickLoginBtn); getYzmBtn.onClick.AddListener(OnClickGetYzmBtn); image.gameObject.SetActive(false); + + //初始状态:禁用登录按钮 + loginBtn.interactable = false; } + + // 检查账号输入是否合法 + private void OnAccountInputChanged(string input) + { + if (!IsValidAccount(input)) + { + id.text = ""; + Debug.Log("账号输入不合法!"); + } + + UpdateLoginButtonState(); + } + // 更新登录按钮状态 + private void UpdateLoginButtonState() + { + // 按钮启用条件:账号不为空且密码长度 >= 6 + loginBtn.interactable = !string.IsNullOrEmpty(id.text) && pwd.text.Length >= 6; + } + // 检查密码输入是否合法 + private void OnPasswordInputChanged(string input) + { + if (input.Length > 16) + { + pwd.text = input.Substring(0, 16); // 超过16位,截取前16位 + Debug.Log("密码长度超过限制!"); + } + + // 设置为显示 '*' 的形式 + string hiddenPassword = new string('*', pwd.text.Length); + pwd.textComponent.text = hiddenPassword; + + UpdateLoginButtonState(); + } + + public void OnClickGetYzmBtn() { @@ -112,5 +154,23 @@ public class LoginPanel : Base //ReadRoom.instance.head(); } + // 账号合法性验证 + private bool IsValidAccount(string input) + { + if (string.IsNullOrEmpty(input)) return false; + // 检查长度限制 + if (input.Length < 3 || input.Length > 20) return false; + + // 检查首字符是否为字母 + if (!char.IsLetter(input[0])) return false; + + // 检查是否只包含字母、数字和下划线 + foreach (char c in input) + { + if (!char.IsLetterOrDigit(c) && c != '_') return false; + } + + return true; + } }