Spanish TextField Format: Números o letras

soyVB6

Active Member
Licensed User
Estoy usando una función con bastante código para lograr algo sencillo, Obligar TextField que solo se pueda escribir Números o Letras.

Aquí mi función:

B4X:
Public Sub TextFormat(tx As TextField,numeros As Boolean) As String 
Dim newCd As String=tx.Text
If admite.IsInitialized = False Then
admite.Initialize
admite.Put("a","a")
admite.Put("b","b")
admite.Put("c","c")
admite.Put("d","d")
admite.Put("e","e")
admite.Put("f","f")
admite.Put("g","g")
admite.Put("h","h")
admite.Put("i","i")
admite.Put("j","j")
admite.Put("k","k")
admite.Put("l","l")
admite.Put("m","m")
admite.Put("n","n")
admite.Put("ñ","ñ")
admite.Put("o","o")
admite.Put("p","p")
admite.Put("q","q")
admite.Put("r","r")
admite.Put("s","s")
admite.Put("t","t")
admite.Put("u","u")
admite.Put("v","v")
admite.Put("w","w")
admite.Put("x","x")
admite.Put("y","y")
admite.Put("z","z")
admite.Put("0","0")
admite.Put("1","1")
admite.Put("2","2")
admite.Put("3","3")
admite.Put("4","4")
admite.Put("5","5")
admite.Put("6","6")
admite.Put("7","7")
admite.Put("8","8")
admite.Put("9","9")
admite.Put(" "," ")
admite.Put(".",".")
admite.Put("@","@")
Else
Dim k As String=admite.Get("a")
If k=="null" Then
admite.Put("a","a")
admite.Put("b","b")
admite.Put("c","c")
admite.Put("d","d")
admite.Put("e","e")
admite.Put("f","f")
admite.Put("g","g")
admite.Put("h","h")
admite.Put("i","i")
admite.Put("j","j")
admite.Put("k","k")
admite.Put("l","l")
admite.Put("m","m")
admite.Put("n","n")
admite.Put("ñ","ñ")
admite.Put("o","o")
admite.Put("p","p")
admite.Put("q","q")
admite.Put("r","r")
admite.Put("s","s")
admite.Put("t","t")
admite.Put("u","u")
admite.Put("v","v")
admite.Put("w","w")
admite.Put("x","x")
admite.Put("y","y")
admite.Put("z","z")
admite.Put("0","0")
admite.Put("1","1")
admite.Put("2","2")
admite.Put("3","3")
admite.Put("4","4")
admite.Put("5","5")
admite.Put("6","6")
admite.Put("7","7")
admite.Put("8","8")
admite.Put("9","9")
admite.Put(" "," ")
admite.Put(".",".")
admite.Put("@","@")
End If
End If

'en caso de ser solo numeros, Limpiamos el map
If numeros=True Then
        admite.Clear
        admite.Put("0","0")
        admite.Put("1","1")
        admite.Put("2","2")
        admite.Put("3","3")
        admite.Put("4","4")
        admite.Put("5","5")
        admite.Put("6","6")
        admite.Put("7","7")
        admite.Put("8","8")
        admite.Put("9","9")
End If
 

For i = 0 To tx.Text.length -1
Dim ncd,eli As String
eli=tx.Text.charat(i)
ncd = admite.Get(eli.ToLowerCase)

If ncd=="null" Then
Log("caracter invalido "&eli)
newCd= newCd.Replace(eli,"")
End If
Next

tx.Text= newCd
tx.SetSelection(tx.Text.Length,tx.Text.Length)

Return newCd

End Sub

Mientras escribo verifico si hay algún carácter no valido en mi map admite
B4X:
tx1_TextChanged (Old As String, New As String)
TextFormat(reg_tx1,True) 'Solo numeros   
End Sub

tx2_TextChanged (Old As String, New As String)
TextFormat(reg_tx2,True) 'Solo LETRAS 
End Sub

¿ Otra forma de hacerlo ?
 

JCO

Active Member
Licensed User
Longtime User
A bote pronto se me ocurre algo así:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    
    Private lstLetras As List = Array("a","b", ... "z", "0", ... "9")
    Private lstNumeros As List = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
    
End Sub

Public Sub TextFormat(tx As TextField,numeros As Boolean) As String
    Dim newCd As String=tx.Text
    Dim lst As List
    
    If numeros Then
        lst = lstNumeros
    Else
        lst = lstLetras
    End If
    
    For i = 0 To tx.Text.length -1
        Dim ncd, eli As String
        eli = tx.Text.ToLowerCase.CharAt(i)
        If lst.IndexOf(eli) = -1 Then
            Log("caracter invalido "&eli)
            newCd = newCd.Replace(eli, "")
        End If
    Next
    
    tx.Text= newCd
    tx.SetSelection(tx.Text.Length, tx.Text.Length)
    
    Return newCd

End Sub

Tiene la ventaja de ser más corto y creo que más fácil de leer/modificar, pero me da la impresión de que el rendimiento no será mucho mejor

Saludos,
Julio
 
Top