B4J Question TextField limit text length

Pendrush

Well-Known Member
Licensed User
Longtime User
How can I limit text length in TextField, for example to max 4 characters. Also I need to limit TextField to numbers only?

EDIT:
Solution if someone need it, with 3 characters limit:
B4X:
Sub txt1_TextChanged (Old As String, New As String)
    Dim sb As StringBuilder
    sb.Initialize
    For i = 0 To New.Length - 1
        If "0123456789".IndexOf(New.CharAt(i)) > -1 Then
        sb.Append(New.CharAt(i))
        End If
    Next
    If sb.ToString.Length > 3 Then
        txt1.Text = sb.ToString.SubString2(0,3)
    Else
        txt1.Text = sb.ToString
    End If
    txt1.SetSelection(txt1.Text.Length, txt1.Text.Length)
End Sub
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…