@@ -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