EditText maximum characters best practice

Brad

Active Member
Licensed User
Longtime User
I'm needing to limit the number of characters a user can input. Since I wasn't able to find any info in the docs I created my own routine which works great except for one thing. It allows the user to exceed the max length by one character before removing it. I would much rather have the user unable to enter anymore characters beyond the max. It looks kinda goofy.

B4X:
Sub EditText1_TextChanged (Old As String, New As String)
Dim pline As String
   pline = EditText1.Text
If pline.Length > 40 Then
   EditText1.Text = pline.SubString2(0,39)
   EditText1.SelectionStart = 39
   ToastMessageShow("40 character limit",False)
End If
End Sub
 

Brad

Active Member
Licensed User
Longtime User
Found my problem. Changed the 40 to 39. Didn't realize length is zero based. Now it works correctly.

B4X:
Sub EditText1_TextChanged (Old As String, New As String)
Dim pline As String
   pline = EditText1.Text
If pline.Length > 39 Then
   EditText1.Text = pline.SubString2(0,39)
   EditText1.SelectionStart = 39
   ToastMessageShow("40 character limit",False)
End If
End Sub
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Hi Brad,

Thanks for the code, I'm using it now.
But without the "ToastMessageShow" line.

note. maybe you can put this in the Tutorial section.
In case someone else looking for it.

grtz.
Asmoro
 
Upvote 0

Brad

Active Member
Licensed User
Longtime User
Hey Asmoro,
Great that you are using the code.. I'm replacing the toast message with a short phone vibrate. :)
 
Upvote 0
Top