Help with text mask

astf

Member
Licensed User
Longtime User
I need to make two masks for input text...
one for currency and other one for date ...

but i can't do this...

Someone can help me?

ps: sorry for my english ... I'm brazilian ;)

B4X:
Sub EditText1_TextChanged (Old As String, New As String)
   Dim Saida As String
   Dim res As String
   Dim pos As Int : pos = 0
   Saida = New.Replace(".","")
   
   res = New
      
'   If Saida.Length < 3 Then
'      res = Saida.SubString(2) & "." & Saida.SubString(2)
'   End If
   
   If Saida.Length >= 4 Then
      Saida = Saida.Replace(",","")
      res = NumberFormat(Saida,0,1)
      res = res.Replace(",",".")
      EditText1.SelectionStart = Saida.Length + pos
      pos = 1
   End If
   
   If New <> Old Then
      EditText1.Text = res
      EditText1.SelectionStart = Saida.Length + pos
   End If
   
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't recommend you to use TextChanged event to check that the date string is valid. The user experience will be very bad.
You should instead validate the string when you do something with it.

To only allow decimal numbers you should use:
B4X:
EditText1.InputType = EditText1.INPUT_TYPE_DECIMAL_NUMBERS

The easiest way to validate the date is to try and parse it and use Try Catch to catch the error if it is not valid.
 
Upvote 0

astf

Member
Licensed User
Longtime User
Hi Erel,
I'm doing a program for collectors of money for companies that use our system (windows), and this is kind of hard headed people ...
I need to give all chewed...

I make the check date with the TextChanged, but i'cant erase the text inside the edittext... =(

Can you help me?

B4X:
Sub EditText1_TextChanged (Old As String, New As String)
   Dim data1 As String
   If New.Length = 3 Then
      data1 = New.SubString2(0,2) & "/" & New.SubString2(2,3)
      EditText1.Text = data1
      EditText1.SelectionStart = 4
   Else If New.Length = 5 Then
      data1 = New.SubString2(0,5) & "/"
      EditText1.Text = data1
      EditText1.SelectionStart = 6
   Else If New.Length = 11 Then
      EditText1.Text = Old
      EditText1.SelectionStart = 10
   End If   
End Sub
 
Upvote 0

adrianomurta

Member
Licensed User
Longtime User
hi,
see below
demo for mobile fone mask


Sub edtCelular_TextChanged (Old As String, New As String)
Dim current As String
current = New

Dim positioning(12) As Int
'positioning(1) = 1, 2, 3, 6, 7, 8, 9, 11, 12, 13, 14 ) As Int
'(99) 1234-1234
positioning(0) = 1
positioning(1) = 2 '1
positioning(2) = 3 '2
positioning(3) = 6 ' 3
positioning(4) = 7
positioning(5) = 8
positioning(6) = 9
positioning(7) = 11
positioning(8) = 12
positioning(9) = 13
positioning(10) = 14

If (isUpdating) Then
isUpdating = False
Return
End If

Dim number As String

number=""
Dim letra As String
Dim lista As String
lista="0123456789"
For i=0 To current.length-1
letra=current.CharAt(i)
If lista.IndexOf(letra) >=0 Then
number=number & letra
End If


Next
Dim maxNumberLength As Int
maxNumberLength=11

If (number.length > 10) Then
number = number.substring2(0, 10)
'number.sub
End If
Dim length As Int
length = number.length

' Pad the number To 10 characters...
Dim paddedNumber As String
paddedNumber = padNumber(number, maxNumberLength)
' Split phone number into parts... /
Dim ddd,part1,part2 As String
Try
ddd = paddedNumber.substring2(0, 2)
Catch
End Try

Try
part1 = paddedNumber.substring2(2, 6)
Catch
End Try

Try
part2 = paddedNumber.substring2(6, 10)
Catch
End Try
' build the masked phone number... */
Dim Phone As String

Phone = "(" & ddd & ") " & part1 & "-" & part2

isUpdating = True
edtCelular.Text=Phone
edtCelular.SelectionStart = positioning( length )
End Sub

Sub padNumber( number As String, maxLength As Int) As String
'String padded = new String(number);
Dim padded As String
Dim i As Int
padded=number
For i = 0 To maxLength - number.length
padded = padded & " "
Next
Return padded

End Sub

amigo, se precisar tenho outras mascaras
sou brasileiro,ok (31) 94136747
 
Upvote 0
Top