B4J Code Snippet TextField Numeric Input only

Jmu5667

Well-Known Member
Licensed User
Hi all

Here is how I handle numbers only input for a text field.


B4X:
Sub txtField_TextChanged (Old As String, New As String)
   
   Dim update As Boolean
   
   Try
       Dim text As String = Regex.Replace("\D",New,"")
       If New <> text Then
           update = True
       End If
   Catch
       Log(LastException.Message)
   End Try
   
   If text.Length > 15 Then   ' 15 is the max length allowed you can change this :)
       text = text.SubString2(0,15)
       update = True       
   End If
   
   If update Then
       txtField.Text = text
       txtField.SetSelection(text.Length,text.Length)
   End If


End Sub
Regards

John
 
Top