Text Entry Controlled Input - Forum

Forum Navigation
You need to log in to create posts and topics.

Text Entry Controlled Input

@neobook asked here https://visualneo.com/forum/topic/text-entry-masic-problem about a TextEntry Box that restricts user input to a maximum of 5 integers, 2 decimals and 1 decimal separator.

Here is one solution ...

1) the code goes in the Text Change section of the Text Entry Box; it is invoked after each character is typed by the user

2) it makes sure ...

a) only numbers (0 to 9) or the DecimalSeparator are entered; the DecimalSeparator can be defined at the start of the code; currently set to dot, change to comma if in Europe)

b) only one Decimal Separator is entered

c) maximum of 5 integers and 3 decimals are entered

... only allow (upto 5 integers and 2 decimals)
SetVar "[ControlledInputMaxIntegers]" "5"
SetVar "[ControlledInputMaxDecimals]" "2"
SetVar "[ControlledInputDecimalCharacter]" "!."

StrLen "[ControlledInput]" "[ControlledInputLen]"
If "[ControlledInputLen]" "=" "0"
   Return
EndIf

... check each character ... must be 0 to 9 or [ControlledinputDecimalCharacter]
Loop "1" "[ControlledInputLen]" "[thisCharPos]"
   SubStr "[ControlledInput]" "[thisCharPos]" "1" "[thisChar]"
   IfEx "([thisChar] >= 0 AND [thisChar] <= 9) OR [thisChar] = [ControlledinputDecimalCharacter]"
   ... valid; do nothing
   Else
      ... not approved
      AlertBox "Error1" "[thisChar] at position [thisCharPos] is not allowed"
      ... remove from input
      StrReplace "[ControlledInput]" "[thisChar]" "" "[ControlledInput]" ""
      Return
   EndIf
EndLoop

... ensure no more than one [ControlledinputDecimalCharacter] character
Setvar "[currentDecimalCharacterCount]" "0"
Loop "1" "[ControlledInputLen]" "[thisCharPos]"
   SubStr "[ControlledInput]" "[thisCharPos]" "1" "[thisChar]"
   If "[thisChar]" "=" "[ControlledInputDecimalCharacter]"
      Math "1+[currentDecimalCharacterCount]" "0" "[currentDecimalCharacterCount]"
      If "[currentDecimalCharacterCount]" ">" "1"
         AlertBox "Error2" "Only one [ControlledInputDecimalCharacter] character allowed; please remove extra character(s)"
         Return
      EndIf
   EndIf
EndLoop

... split into integers and decimals
StrParse "[ControlledInput]" "[ControlledInputDecimalCharacter]" "[ControlledInputItem]" "[ControlledInputItemCount]"
... check integer length
StrLen "[ControlledInputItem1]"  "[ControlledInputItem1Len]"
If "[ControlledInputItem1Len]" ">" "[ControlledInputMaxIntegers]"
   AlertBox "Error3" "Only [ControlledinputMaxIntegers] integers allowed; please remove extra digit(s)"
   Return
Endif
... check decimal length
StrLen "[ControlledInputItem2]"  "[ControlledInputItem2Len]"
If "[ControlledInputItem2Len]" ">" "[ControlledInputMaxDecimals]"
   AlertBox "Error4" "Only [ControlledinputMaxDecimals] decimals allowed; please remove extra digit(s)"
   Return
Endif

The above code is also included in attached text file (in case automatic translation messes up the code within the post) ... I used the PUB extension because this forum does not allow .dat or .txt files to be uploaded; please rename the file after download !!!

Uploaded files:
  • You need to login to have access to uploads.
luishp and neobook have reacted to this post.
luishpneobook

Grande Gaev, funciona de 10. Es lo que buscaba.

Gracias !!