feat(ui): implement login UI validation logic

This commit is contained in:
EmirHanMamak 2026-01-17 13:19:56 +03:00
parent d1745e1af3
commit ea005e4477
2 changed files with 50 additions and 14 deletions

View file

@ -451,8 +451,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3955dbe39e234b7a84bffd5f053855ff, type: 3} m_Script: {fileID: 11500000, guid: 3955dbe39e234b7a84bffd5f053855ff, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
connectButton: {fileID: 0} loginButton: {fileID: 489976505}
sendButton: {fileID: 0} usernameInputField: {fileID: 535676077}
passwordInputField: {fileID: 234502803}
--- !u!1 &184757597 --- !u!1 &184757597
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1346,7 +1347,7 @@ MonoBehaviour:
m_PressedTrigger: Pressed m_PressedTrigger: Pressed
m_SelectedTrigger: Selected m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled m_DisabledTrigger: Disabled
m_Interactable: 1 m_Interactable: 0
m_TargetGraphic: {fileID: 489976506} m_TargetGraphic: {fileID: 489976506}
m_OnClick: m_OnClick:
m_PersistentCalls: m_PersistentCalls:

View file

@ -1,5 +1,7 @@
using System; using System;
using System.Text.RegularExpressions;
using _Hub.Scripts.Network; using _Hub.Scripts.Network;
using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -7,26 +9,59 @@ namespace _Hub.Scripts.UI
{ {
public class LoginUI : MonoBehaviour public class LoginUI : MonoBehaviour
{ {
[SerializeField] private Button connectButton; #region Variables
[SerializeField] private Button sendButton;
private const int _maxUsernameLength = 15;
private const int _maxPasswordLength = 15;
[SerializeField] private Button loginButton;
[SerializeField] private TMP_InputField usernameInputField;
[SerializeField] private TMP_InputField passwordInputField;
private string _username = String.Empty;
private string _password = String.Empty;
#endregion
#region Unity
private void Awake() private void Awake()
{ {
connectButton.onClick.AddListener(OnConnectButton); loginButton.onClick.AddListener(OnLoginClick);
sendButton.onClick.AddListener(OnSendButton); usernameInputField.onValueChanged.AddListener(UpdateUsername);
passwordInputField.onValueChanged.AddListener(UpdatePassword);
} }
private void OnLoginClick()
private void OnConnectButton()
{ {
NetworkClient.Instance.Connect();
Debug.Log("Connect Button");
} }
private void OnSendButton() private void UpdateUsername(string username)
{ {
NetworkClient.Instance.SendServer("This is what i send"); _username = username;
Debug.Log("Send Button"); Debug.Log("Username:" + username);
ValidateAndUpdateUI();
} }
private void UpdatePassword(string password)
{
_password = password;
Debug.Log("Password:" + password);
ValidateAndUpdateUI();
}
private void ValidateAndUpdateUI()
{
var usernameRegex = Regex.Match(_username, "^[a-zA-Z0-9]+$");
var interactable = (!string.IsNullOrWhiteSpace(_username) && !string.IsNullOrWhiteSpace(_password) &&
(_maxUsernameLength >= _username.Length && _maxPasswordLength >= _password.Length) &&
usernameRegex.Success);
EnableLoginButton(interactable);
}
private void EnableLoginButton(bool interactable)
{
loginButton.interactable = interactable;
}
#endregion
} }
} }