Skip to content

Commit 955387c

Browse files
sanitized text in input/IP & port fields on input change callback (#298)
* sanitized text in input/IP & port fields on input change callback * adding underscore to allowed chars to input field * renaming variable * re-adding oninputchange callbacks on prefab
1 parent bd41772 commit 955387c

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

Assets/BossRoom/Prefabs/UI/PopupPanel.prefab

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,19 @@ MonoBehaviour:
733733
m_Calls: []
734734
m_OnValueChanged:
735735
m_PersistentCalls:
736-
m_Calls: []
736+
m_Calls:
737+
- m_Target: {fileID: 2699227445768669173}
738+
m_TargetAssemblyTypeName: BossRoom.Visual.PopupPanel, BossRoom.Client
739+
m_MethodName: SanitizePortText
740+
m_Mode: 1
741+
m_Arguments:
742+
m_ObjectArgument: {fileID: 0}
743+
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
744+
m_IntArgument: 0
745+
m_FloatArgument: 0
746+
m_StringArgument:
747+
m_BoolArgument: 0
748+
m_CallState: 2
737749
m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
738750
m_CustomCaretColor: 0
739751
m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
@@ -968,7 +980,7 @@ MonoBehaviour:
968980
m_faceColor:
969981
serializedVersion: 2
970982
rgba: 4294967295
971-
m_fontSize: 21.55
983+
m_fontSize: 18
972984
m_fontSizeBase: 32
973985
m_fontWeight: 400
974986
m_enableAutoSizing: 1
@@ -2069,7 +2081,19 @@ MonoBehaviour:
20692081
m_Calls: []
20702082
m_OnValueChanged:
20712083
m_PersistentCalls:
2072-
m_Calls: []
2084+
m_Calls:
2085+
- m_Target: {fileID: 2699227445768669173}
2086+
m_TargetAssemblyTypeName: BossRoom.Visual.PopupPanel, BossRoom.Client
2087+
m_MethodName: SanitizeInputText
2088+
m_Mode: 1
2089+
m_Arguments:
2090+
m_ObjectArgument: {fileID: 0}
2091+
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
2092+
m_IntArgument: 0
2093+
m_FloatArgument: 0
2094+
m_StringArgument:
2095+
m_BoolArgument: 0
2096+
m_CallState: 2
20732097
m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
20742098
m_CustomCaretColor: 0
20752099
m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
@@ -2637,7 +2661,7 @@ MonoBehaviour:
26372661
m_faceColor:
26382662
serializedVersion: 2
26392663
rgba: 4294967295
2640-
m_fontSize: 30.35
2664+
m_fontSize: 35.05
26412665
m_fontSizeBase: 32
26422666
m_fontWeight: 400
26432667
m_enableAutoSizing: 1

Assets/BossRoom/Scripts/Client/UI/PopupPanel.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class PopupPanel : MonoBehaviour
5959

6060
private const string k_DefaultConfirmText = "OK";
6161

62+
static readonly char[] k_InputFieldIncludeChars = new[] { '.', '_' };
63+
6264
/// <summary>
6365
/// Setup this panel to be a panel view to have the player enter the game, complete with the ability for the player to
6466
/// cancel their input and requests.
@@ -122,6 +124,45 @@ private void OnConfirmClick()
122124
m_ConfirmFunction.Invoke(m_InputField.text, portNum, m_NameDisplay.GetCurrentName(), (OnlineMode)m_OnlineModeDropdown.value);
123125
}
124126

127+
/// <summary>
128+
/// Sanitize user port InputField box allowing only alphanumerics, plus any matching chars, if provided.
129+
/// </summary>
130+
/// <param name="dirtyString"> string to sanitize. </param>
131+
/// <param name="includeChars"> Array of chars to include. </param>
132+
/// <returns> Sanitized text string. </returns>
133+
static string Sanitize(string dirtyString, char[] includeChars = null)
134+
{
135+
var result = new StringBuilder(dirtyString.Length);
136+
foreach (char c in dirtyString)
137+
{
138+
if (char.IsLetterOrDigit(c) ||
139+
(includeChars != null && Array.Exists(includeChars, includeChar => includeChar == c)))
140+
{
141+
result.Append(c);
142+
}
143+
}
144+
145+
return result.ToString();
146+
}
147+
148+
/// <summary>
149+
/// Added to the InputField component's OnValueChanged callback for the Room/IP UI text.
150+
/// </summary>
151+
public void SanitizeInputText()
152+
{
153+
var inputFieldText = Sanitize(m_InputField.text, k_InputFieldIncludeChars);
154+
m_InputField.text = inputFieldText;
155+
}
156+
157+
/// <summary>
158+
/// Added to the InputField component's OnValueChanged callback for the Port UI text.
159+
/// </summary>
160+
public void SanitizePortText()
161+
{
162+
var inputFieldText = Sanitize(m_PortInputField.text);
163+
m_PortInputField.text = inputFieldText;
164+
}
165+
125166
/// <summary>
126167
/// Called when the user clicks on the cancel button when in a mode where the player is expecting to input something.
127168
/// Primary responsibility for this method is to reset the UI state.

0 commit comments

Comments
 (0)