B4J Question Regex expression for decimal numbers

stevetheframe

Member
Licensed User
Longtime User
I'm using this code for validating input in a textfield. (In the TextChanged event).
It gives numbers only (integers).

B4X:
Try
        Dim text As String = Regex.Replace("\D",New,"")
        If New <> text Then
            updatetext = True
        End If
    Catch

Is there a Regex expression that will allow for decimal numbers?
 

udg

Expert
Licensed User
Longtime User
Google for "regex decimal numbers". There are several ready-made examples
First fews returned by a quick search:
 
Upvote 0

stevetheframe

Member
Licensed User
Longtime User
Google for "regex decimal numbers". There are several ready-made examples
First fews returned by a quick search:
Thanks for the pointers. Haven't got the code working yet, but I'll keep plugging away.
 
Upvote 0

stevetheframe

Member
Licensed User
Longtime User
This is already implemented in B4XInputTemplate (XUI Views).

Thanks, that would work, but I didn't want another dialogue popping up to interrupt the user.
I have come up with the following (crude) code in the KeyChanged event.
B4X:
If txtX.Text = "" Then
      Return
 Else
     Dim chrtolookat As String = txtX.Text.CharAt(txtX.Text.Length - 1)
    Select chrtolookat
        Case "0","1","2","3","4","5","6","7","8","9"
            Return
        Case "."
            Return
        Case Else
            txtX.Text = txtX.Text.SubString2(0,txtX.Text.Length - 1)
            txtX.SetSelection(txtX.Text.Length,txtX.Text.Length)
      End Select
 End If
Of course, it allows a decimal point to be entered multiple times., but perhaps I can trap that a bit further down the line.
 
Upvote 0

stevetheframe

Member
Licensed User
Longtime User
Thanks, that would work, but I didn't want another dialogue popping up to interrupt the user.
I have come up with the following (crude) code in the KeyChanged event.
B4X:
If txtX.Text = "" Then
      Return
Else
     Dim chrtolookat As String = txtX.Text.CharAt(txtX.Text.Length - 1)
    Select chrtolookat
        Case "0","1","2","3","4","5","6","7","8","9"
            Return
        Case "."
            Return
        Case Else
            txtX.Text = txtX.Text.SubString2(0,txtX.Text.Length - 1)
            txtX.SetSelection(txtX.Text.Length,txtX.Text.Length)
      End Select
End If
Of course, it allows a decimal point to be entered multiple times., but perhaps I can trap that a bit further down the line.

Sorted! This code does the job and has the advantage of being readily understandable.
B4X:
Sub txtX_TextChanged (Old As String, New As String)
    
    If txtX.Text = "" Then
        Return
    Else
        Dim chrtolookat As String = txtX.Text.CharAt(txtX.Text.Length - 1)
        Select chrtolookat
            Case "0","1","2","3","4","5","6","7","8","9"
                Return
            Case "."
                If Old.Contains(".") Then
                    txtX.Text = txtX.Text.SubString2(0,txtX.Text.Length - 1)
                    txtX.SetSelection(txtX.Text.Length,txtX.Text.Length)
                Else
                    Return
                End If
            Case Else
                txtX.Text = txtX.Text.SubString2(0,txtX.Text.Length - 1)
                txtX.SetSelection(txtX.Text.Length,txtX.Text.Length)
        End Select
    End If
    
End Sub
 
Upvote 0
Top