мне нужно добавить в этот код проверку для первых символов что они не больше 24 часов и…
- User
мне нужно добавить в этот код проверку для первых символов что они не больше 24 часов и после них установить " : (мин)" using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;
public class TimeInputField : MonoBehaviour { [SerializeField] private TMP_InputField inputField;
private int hours; private int minutes; private void Start() { inputField.onValueChanged.AddListener(OnValueChanged); } private void OnValueChanged(string value) { // Check the input length if (value.Length >= 4) { // Extract hours and minutes string hoursStr = value.Substring(0, 2); string minutesStr = value.Substring(2, 2); int hours, minutes; // Try parsing the hours and minutes if (int.TryParse(hoursStr, out hours) && int.TryParse(minutesStr, out minutes)) { // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", hours, minutes); inputField.text = formattedValue; return; } } - Assistant
To add a validation to check if the first two characters are not greater than 24 hours, you can modify the
OnValueChangedmethod as follows:private void OnValueChanged(string value) { // Check the input length if (value.Length >= 4) { // Extract hours and minutes string hoursStr = value.Substring(0, 2); string minutesStr = value.Substring(2, 2); int hours, minutes; // Try parsing the hours and minutes if (int.TryParse(hoursStr, out hours) && int.TryParse(minutesStr, out minutes)) { // Check if hours is not greater than 24 if (hours > 24) { hours = 24; } // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", hours, minutes); inputField.text = formattedValue; return; } } }This code checks if the parsed
hoursvalue is greater than 24 and if so, sets it to 24 to ensure it doesn't exceed the maximum limit. - User
и еще нужна проверка чтобы можно ввести только цифры
- Assistant
To add a validation to allow only numeric input in the
inputField, you can use theRegexclass to check if the input contains only digits. Here's an updated version of theOnValueChangedmethod that includes this validation:private void OnValueChanged(string value) { // Check if the input contains only digits if (!Regex.IsMatch(value, @"^\d*$")) { // Remove any non-digit characters from the input value = Regex.Replace(value, @"\D", ""); inputField.text = value; return; } // Check the input length if (value.Length >= 4) { // Extract hours and minutes string hoursStr = value.Substring(0, 2); string minutesStr = value.Substring(2, 2); int hours, minutes; // Try parsing the hours and minutes if (int.TryParse(hoursStr, out hours) && int.TryParse(minutesStr, out minutes)) { // Check if hours is not greater than 24 if (hours > 24) { hours = 24; } // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", hours, minutes); inputField.text = formattedValue; return; } } } - User
еще нудно поменять проверку где проверяется длина ввода, нужно чтобы в начале проверялось первые 2 символа, после их ввода устанавливалось (ч) : и дальше мы вводили еще 2 символа
- Assistant
To change the validation to check the first two characters and then set "(ч) :" before allowing input of the next two characters, you can modify the logic in the
OnValueChangedmethod as follows:private void OnValueChanged(string value) { // Check if the input contains only digits if (!Regex.IsMatch(value, @"^\d*$")) { // Remove any non-digit characters from the input value = Regex.Replace(value, @"\D", ""); inputField.text = value; return; } // Check the input length for the first two characters if (value.Length == 2) { int hours; // Try parsing the hours if (int.TryParse(value, out hours)) { // Check if hours is not greater than 24 if (hours > 24) { hours = 24; } // Update the input field with the formatted hours inputField.text = string.Format("{0:D2} (ч) :", hours); return; } } // Check the input length for the next two characters if (value.Length >= 4) { int minutes; // Extract minutes string minutesStr = value.Substring(2, 2); // Try parsing the minutes if (int.TryParse(minutesStr, out minutes)) { // Apply the desired format string formattedValue = string.Format("{0} : {1:D2} (мин)", inputField.text, minutes); inputField.text = formattedValue; return; } } } - User
и еще мне нужна проверка если когда мы вводим больше минут чем 60 то мы прибавляем это к часам
- Assistant
- User
похоже тут ошибка ведь когда приходит значение в этот метод то у меня оно даже не доходит до проверки на количество символов
- Assistant
I apologize for the oversight. You're correct that the code I provided was missing a crucial part to handle the input before the length check. Here's an updated version that includes the necessary changes:
private void OnValueChanged(string value) { // Check if the input contains only digits if (!Regex.IsMatch(value, @"^\d*$")) { // Remove any non-digit characters from the input value = Regex.Replace(value, @"\D", ""); inputField.text = value; return; } // Extract hours and minutes string hoursStr = value.Length >= 2 ? value.Substring(0, 2) : "00"; string minutesStr = value.Length >= 4 ? value.Substring(2, 2) : "00"; int hours, minutes; // Try parsing the hours and minutes if (int.TryParse(hoursStr, out hours) && int.TryParse(minutesStr, out minutes)) { // Check if minutes is greater than 60 if (minutes > 60) { int exceedingMinutes = minutes - 60; hours += (exceedingMinutes / 60); // Add exceeding minutes to hours minutes = exceedingMinutes % 60; // Set minutes to the remaining exceeding minutes // Check if hours is not greater than 24 if (hours > 24) { hours = 24; } } // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", hours, minutes); inputField.text = formattedValue; } }