Android Question use regex to filter B4XFloatTextField

tsteward

Well-Known Member
Licensed User
Longtime User
I like how in the B4XInputTemplate way you can use regex to signal when input is correct with the red or green outline.
Is there a way I can do this with a B4XFloatTextField
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Since B4XFloatTextField calls back the Textfield1_TextChanged in your module, you can do the validation yourself.
Copy lines 66-85 from B4XInputTemplate.bas (located C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries).
 
Last edited:
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
The actual textfield is the first view in B4XFloatTextField1.mBase.

Based on https://www.b4x.com/android/forum/attachments/xui-views-example-zip.84136/


B4X:
Sub B4XFloatTextField1_TextChanged (Old As String, New As String)
    Dim actualTextfield As B4XView = B4XFloatTextField1.mBase.GetView(0)
    'New is same as actualTextfield.Text - here for testing random entries are considered wrong
    If Rnd(0,2) = 0 And New <> "Abcde" Then
        Log("Wrong answer " & New)
        actualTextfield.SetColorAndBorder(XUI.Color_White, 3, XUI.Color_Red, 2)
    Else
        actualTextfield.SetColorAndBorder(XUI.Color_White, 1, XUI.Color_lightgray, 2)
    End If
    'Other stuff
End Sub
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Is there a way I can do this with a B4XFloatTextField
Since you specify that you like to see RegEx to validate the input, I thought perhaps you like to see this:
B4X:
Sub B4XFloatTextField1_TextChanged (Old As String, New As String)
    Dim B4Xtf As B4XView = B4XFloatTextField1.mBase.GetView(0)
    If IsValid(New)  Then
        B4Xtf.SetColorAndBorder(XUI.Color_White, 3dip, XUI.Color_Green, 2dip)
        B4XFloatTextField1.mBase.GetView(3).TextColor=XUI.Color_Green '4th view: GetView(3) is the accept icon
        B4XFloatTextField1.mBase.GetView(3).Enabled=True
        B4XFloatTextField1.mBase.GetView(2).TextColor=XUI.Color_Gray  '3rd view GetView(2) is the x not accept icon
        B4XFloatTextField1.mBase.GetView(2).Enabled=False
    Else
        B4Xtf.SetColorAndBorder(XUI.Color_White, 3dip, XUI.Color_Red, 2dip)
        B4XFloatTextField1.mBase.GetView(2).TextColor=XUI.Color_Red  '3rd view GetView(2) is the x not accept icon
        B4XFloatTextField1.mBase.GetView(2).Enabled=True
        B4XFloatTextField1.mBase.GetView(3).TextColor=XUI.Color_Gray '
        B4XFloatTextField1.mBase.GetView(3).Enabled=False
        'Log(B4XFloatTextField1.mBase.NumberOfViews)   
    End If
End Sub
B4X:
Sub IsValid(entry As String) As Boolean  'allows only letters, digits, spaces and apostrophes
    If entry.Length = 0 Then Return False
    Dim regexPattern As StringBuilder
    regexPattern.Initialize
    regexPattern.Append("^")
    regexPattern.Append("[\w \d']*$")
    Return Regex.IsMatch(regexPattern.ToString, entry)
End Sub
 
Upvote 0
Top