B4J Question How Customize(Formated) TextField For Numbers??

behnam_tr

Active Member
Licensed User
Longtime User
I want to format the textfield in such a way that only
Whole numbers up to 5 digits
and accept decimal numbers up to 5 digits
Also, the number of digits after the decimal point should not be more than 3
Also, do not accept numbers as 0044, which means the first digit must be greater than zero


B4X:
Sub TextFieldFromatter(TFName As TextField)
    
    Dim tf As TextFormatter
    tf.Initialize(Me,"Numeric",TFName)

End Sub

Sub Numeric_TextValidator(Change As TFChange) As TFChange

    'Get the character that has been changed
    Dim ThisChar As String = Change.Text
    
    'Get the full text as it would be applied to the field
    Dim Text As String = Change.ControlNewText
    
    'Duplicate the full text so we can manipulate it
    Dim FullText As String = Text
    
'    'Check we are using the correct decimal separator
'    If "." <> "." And ThisChar = "." Then Return Null
    
    'Check spece betwen Words
    If ThisChar = " " Then Return Null
    
    'Test there is only one decimal Separator
    If FullText.Replace(".","").Length < FullText.Length - 1 Then Return Null
    
    'Allow '-' and '.' at the beginning
    'If FullText.StartsWith("-") Or FullText.StartsWith(DecimalSeparator) Then FullText = FullText.SubString(1)
    
    'Allow '-' at the beginning (Negative Numbers)
    If FullText.StartsWith("-")  Then FullText = FullText.SubString(1)
    
    'Allow '-' at the beginning (Negative Numbers)
    If FullText.StartsWith("+")  Then Return Null
    
    'Allow '.' at the end
    If FullText.EndsWith(".") Then FullText = FullText.SubString2(0,FullText.Length-1)
    
    'IsNumber allows f and d at the end so override this
    If Regex.IsMatch("[df]",ThisChar) Then Return Null
    
'    'Isnumber requires decimal separator to be ".", so change it for the next test.
'    FullText = FullText.Replace(".",".")

    'What is left should be an empty string(we have removed other characters we want to allow) or a valid number
    If FullText = "" Or IsNumber(FullText) Then Return Change
    
    'Invalidates this change, it will not be applied to the input field.
    Return Null
End Sub
'
 

Attachments

  • sample.zip
    2.8 KB · Views: 62

josejad

Expert
Licensed User
Longtime User
Hi, have you take a look to this internal library?

 
Upvote 0
Top