B4J Question String check brings error

strupp01

Active Member
Licensed User
Longtime User
I'm trying to check a string for invalid characters. The valid characters are 0 to 9 and + and ,
My program goes out with errors. No explanation.
Who can help ?

Sub Process_Globals
Private fx As JFX
Private MainForm As Form

Dim W_Breite_mehrere As TextField

End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show

W_Breite_mehrere.Initialize("")

W_Breite_mehrere.Text = "1.2a2,2+3"
'Alle Eingabefelder auf ungültige Zeichen überprüfen
W_Breite_mehrere.Text = W_Breite_mehrere.Text.Replace(".", ",")
For i = 0 To W_Breite_mehrere.Text.Trim.Length - 1
Log(W_Breite_mehrere.Text.Trim.SubString2(i, i+1))
If W_Breite_mehrere.Text.Trim.SubString2(i, i+1) = "+" Or W_Breite_mehrere.Text.Trim.SubString2(i, i+1) = "," Or (W_Breite_mehrere.Text.Trim.SubString2(i, i+1) >= "0" And W_Breite_mehrere.Text.Trim.SubString2(i, i+1) <= "9") Then
'Alles OK
Else
fx.msgbox(Null,"Die Angaben im Feld mehrere Breiten enthalten ungültige Zeichen !!!" , "Programm-Hinweis")
End If
Next
W_Breite_mehrere.Text = W_Breite_mehrere.Text.Replace(",", ".")

End Sub

upload_2017-7-13_14-4-43.png
 

Attachments

  • Test1.zip
    1 KB · Views: 228
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

1. Don't duplicate the same code multiple times. Write it like this:
B4X:
For i = 0 To W_Breite_mehrere.Text.Trim.Length - 1
Dim s As String = W_Breite_mehrere.Text.Trim.SubString2(i, i+1)
     Log(s)
     If s = "+" Or s = "," Or (s >= "0" And s <= "9") Then

2. The greater (>) operator only works with numbers. So it tries to parse the value stored in 's' as a number and fails.
If you want to compare them based on the ascii value (Unicode code point to be more exact):
B4X:
If s = "+" Or s = "," Or (Asc(s) >= Asc("0") And Asc(s) <= Asc("9")) Then
Or simpler:
B4X:
If Regex.IsMatch("[+,\d]+", W_Breite_mehrere.Text.Trim) Then
     'ok
   Else
     fx.msgbox(Null,"Die Angaben im Feld mehrere Breiten enthalten ungültige Zeichen !!!" , "Programm-Hinweis")
   End If
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
This is not directly answering your question. You can do the same thing if you replace the For/Next with this
B4X:
If Regex.IsMatch("[\d,\+]+", W_Breite_mehrere.Text) = True Then
'Alles OK
...

Regex is a powerful tool for validation (and a lot more) that is underutilised.

...and @Erel has beaten me to it :)
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
I found it myself
B4X:
    If Regex.IsMatch("[\d,+, ,\.]+", W_Breite_mehrere.Text.Trim) Then
       'Alles OK
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
I'm fairly sure that the '+' needs to be escaped (as in the example I provided). I just noticed that Erel's example did not escape the '+' which means in Regex it is expecting a ',' after a digit (I think).

Try replacing the Regex string to
"[\d,\+]+"
 
Upvote 0
Top