iOS Question Mask Text Field XXX.XXX.XXX-XX

Luiz Fernando Orlandini

Active Member
Licensed User
Hi All.

I'm trying to develop a sub to format my text field with the mask XXX.XXX.XXX-XX.

It's acceptable only numbers. This is a point in iPad device, cause I can't discover any way to restrict the input for numbers.

I'm using this code.

B4X:
Sub txtDocumentNumber_TextChanged(OldText As String, NewText As String)
    Log ("OldText " & OldText)
    Log ("NewText " & NewText)
       
    If (DocType = "CPF") Then
        If (NewText.CharAt(NewText.Length - 1) = "1" OR NewText.CharAt(NewText.Length - 1) = "2" OR NewText.CharAt(NewText.Length - 1) = "3" OR _
            NewText.CharAt(NewText.Length - 1) = "4" OR NewText.CharAt(NewText.Length - 1) = "5" OR NewText.CharAt(NewText.Length - 1) = "6" OR _
            NewText.CharAt(NewText.Length - 1) = "7" OR NewText.CharAt(NewText.Length - 1) = "8" OR NewText.CharAt(NewText.Length - 1) ="9" OR _
            NewText.CharAt(NewText.Length - 1) = "0" OR NewText.CharAt(NewText.Length - 1) = "") Then
       
            If OldText.Length > NewText.Length Then
                Return
            End If
           
            If (OldText.Length = 3) Then
                txtDocumentNumber.Text = txtDocumentNumber.Text.SubString2(0, 3) & "." & NewText.Replace(txtDocumentNumber.Text.SubString2(0, 3), "")
            Else If (OldText.Length = 7) Then
                txtDocumentNumber.Text = txtDocumentNumber.Text.SubString2(0, 7) & "." & NewText.Replace(txtDocumentNumber.Text.SubString2(0, 7), "")
            Else If (OldText.Length = 11) Then
                txtDocumentNumber.Text = txtDocumentNumber.Text.SubString2(0, 11) & "-" & NewText.Replace(txtDocumentNumber.Text.SubString2(0, 11), "")
            Else If (NewText.Length > 14) Then
                txtDocumentNumber.Text = txtDocumentNumber.Text.SubString2(0, 14)
            End If
        Else
            txtDocumentNumber.Text = OldText
        End If
    End If
   
    Log("txtDocumentNumber " & txtDocumentNumber.Text)
End Sub
 

ilan

Expert
Licensed User
you want only be able to put numbers in the textfield??
if yes this could help you

B4X:
Sub TextField1_TextChanged (OldText As String, NewText As String)

    Dim c As String = newtext.Replace(oldtext,"")
    If IsNumber(c) = False Then TextField1.Text = TextField1.Text.SubString2(0,TextField1.Text.Length)

End Sub
 

Erel

Administrator
Staff member
Licensed User

ilan

Expert
Licensed User
Is it possible to create a custom keyboard with only the numbers, dash and enter keys?
you can also try this...

B4X:
Sub pc_Click

Dim Id As InputDialog
Dim ret As Int
Id.InputType = Id.INPUT_TYPE_DECIMAL_NUMBERS
Id.HintColor= Colors.LightGray
Id.Hint = "Enter Price"
Id.Input = ""
ret = Id.Show("Please enter price", "Price", "Ok", "","Cancel", Null)

If ret = DialogResponse.POSITIVE  Then
    If Id.Input = "" Then
        pc.Text = "0"
    Else
        pc.Text = NumberFormat2(Id.Input,1,2,2,False)
    End If
Else
    Return
End If 

End Sub
 
Last edited:

Mike1970

Well-Known Member
Licensed User
you can also try this...

B4X:
Sub pc_Click

Dim Id As InputDialog
Dim ret As Int
Id.InputType = Id.INPUT_TYPE_DECIMAL_NUMBERS
Id.HintColor= Colors.LightGray
Id.Hint = "Enter Price"
Id.Input = ""
ret = Id.Show("Please enter price", "Price", "Ok", "","Cancel", Null)

If ret = DialogResponse.POSITIVE  Then
    If Id.Input = "" Then
        pc.Text = "0"
    Else
        pc.Text = NumberFormat2(Id.Input,1,2,2,False)
    End If
Else
    Return
End If 

End Sub
Excuse me for the probably silly question, but where is the InputDialog type in B4i V5.00?
Thank you.
 

ilan

Expert
Licensed User
Thank you for your answer.
I've seen the Filippo's InputDialog function, even fixed by Erel. But still, with that, your sample code does not works.
In the row
B4X:
Dim Id As InputDialog
the compiler still say that the type is missing.
Is there a library to download that I'm not seeing?

Thank you again.
yes you are right, it is a mistake, i have added the b4a version of input dialog :oops:

you can use fillipo input dialog or make your own. sorry for my mistake!
 
Top