Android Question How to avoid negative numbers and maximum values in a numeric "edittext"?

Sergio Castellari

Active Member
Licensed User
Situation:
a) I need to be able to control the entered value:
- Do not allow negative numbers
- Numbers greater than or equal to zero and less than 9,999,999.99
A colleague previously helped me to control only 2 decimals and negative numbers with this code which works ok:

B4X:
Private Sub edtImporte_TextChanged (Old As String, New As String)
    'Nota: En el diseñador, configuro EditText1 para esperar una entrada DECIMAL.
        'En los teclados numéricos, el signo menos requiere dos clics en mi dispositivo Samsung
    If New = "-" Then edtImporte.TextColor = xui.Color_Red
    Dim k As  Int = New.IndexOf(".")
    If k > -1 Then
        If New.Length - k > 3 Then
            Main.Beep.beep  'Hace un pitido
            edtImporte.Text = Old
            edtImporte.SelectionStart = Old.length
        End If
    End If
End Sub

I don't realize how I can "modify" it to NOT allow negative values and control the MAXIMUM allowed value.
Greetings!
 

Sergio Castellari

Active Member
Licensed User
Why "fight" with the user? Let the users enter whatever they like and set the border to be red if the input is invalid. Put the EditText in a Panel (B4XView) and set it with B4XView.SetColorAndBorder.
@Erel I need whether or not to allow certain values.
How can I use an editext (B4XView) with the features you mention?
I am a newbie and I do not know your summary explanation.

Greetings and thanks!
 
Upvote 0
Top