Filter EditText Input

mangojack

Expert
Licensed User
Longtime User
Hi .. There is probably many posts out there regarding this, but I have searched 2 hrs now.

Is there a function or method for only allowing Alpha and Numeric input to an EditText ( Only ABC abc ... 123) or filters to Not allow any special characters. I have played with IME.SetCustomFilter and the results were not to good.

I have seen a few posts previously for precisely this ,but cannot locate anything when needed,
Or a case of coming up with my own filtering function ?

Many Thanks
Cheers mj
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim im As IME
   Dim e As EditText
   e.Initialize("")
   Activity.AddView(e, 10dip, 10dip, 200dip, 100dip)
   Dim sb As StringBuilder
   sb.Initialize
   For i = 48 To 57
      sb.Append(Chr(i)) '0 - 9
   Next
   For i = 65 To 90
      sb.Append(Chr(i)) 'A-Z
   Next
   For i = 97 To 122
      sb.Append(Chr(i)) 'a-z
   Next
   im.SetCustomFilter(e, Bit.OR(e.INPUT_TYPE_TEXT,524288) , sb.ToString) '524288 - no suggestions
End Sub
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Thanks Erel that worked great. I'll put that in the archive for future use.
I'll now try to work out why my previous attempts at something similar did not work.

Many thanks Cheers mj
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@mj: To expand on what Erel offered you, if you want to apply the restriction to several edittext views, you can do the following:
B4X:
'In Globals
Dim IME As IME
Dim sb As StringBuilder


'In Activity_Create
sb.Initialize
For i = 48 To 57
     sb.Append(Chr(i)) '0 - 9
Next
For i = 65 To 90
        sb.Append(Chr(i)) 'A-Z
Next
For i = 97 To 122
        sb.Append(Chr(i)) 'a-z
Next


Sub Mytxt_FocusChanged (HasFocus As Boolean) 'Mytxt is event name for edittext views in question
  If HasFocus Then
   Dim Send As EditText
     Send=Sender
    IME.SetCustomFilter(Send, Bit.OR(Send.INPUT_TYPE_TEXT,524288) , sb.ToString) '524288 - no suggestions
  End If
End Sub
Watch those kangaroos on the highway!
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Thanks Mahares .. Very handy. You've visited Dongara ?

Cheers mj
 
Upvote 0

air cover

Member
Licensed User
Longtime User
An alternative to the front-end filter (great for input!) above is a back end filter (better for handling stored data when you have no control over what was already in the db, such as your phone contact list, etc.). The code below creates a numbers-only string in "filtered" from Phone.Number, but you could give it any input source and then set any output source = filtered after it runs.

B4X:
Dim i As Int
Dim t As String
Dim temp As String
Dim filtered As String
Dim allowedcharacters = "0123456789" As String    'whatever is in this string will be allowed in the filtered string, set as desired for your mask

temp=Phone.Number         'use the temporary variable 'temp' instead of the original source
filtered=""
If temp.length>0 Then      'remove non-# characters such as )(-,.
      For i= 0 To temp.Length-1
          t=temp.SubString2(i,i+1)
          If allowedcharacters.Contains(t) Then
              filtered=filtered & t
          End If
      Next
'filtered now contains your string, but only with your allowed characters
End If
 
Upvote 0

bell30

New Member
Hi!

I need to set 3 EditTexts for User Registration/Login. EditText1 contains the Name of the user - only numbers and letters are allowed. EditText2 contains the password - same as the first one, only numbers and letters are allowed. About EditText3 which contains the Mail of the User, I need to allow "@" and "." as well.
So if I initialize StringBuilder in Activity_Create I should add Chr(46) and Chr(64), but after that EditText1 and EditText2 accept "@" and "." as well. Any help?

Thanks in advance
 
Upvote 0

bell30

New Member
Thank Erel, I will have that in mind.

Since I still haven't bought B4A, I can't see the library. However, I found a solution for now by replacing unwanted characters (in this case "@" and ".") with "".
 
Upvote 0
Top