Jump to content

Allow +/- numbers


Recommended Posts

I have been looking at how to allow only positive and negative numbers into a script (and there is much out there), when I did stumbled upon the attached script which is NOT mine. It seems to do the job of what I was looking for WITH one exception.

IF a user were to accidently hit the "." key and nothing else it would accept it as valid.

So I could only think to control this with what I added between the ;//////  in the script. It works but as a newbie I don't know if that is the only way or if it even efficient. So if someone with greater skills could comment on it, I would appreciate it and learn something.

I just noticed also that it looks like a negative"." would be accepted into the input as well (  -.)  .

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

Global $iDecimal = 3

GUICreate("Input Filter", 300, 30, -1, -1)
Global $inTest = GUICtrlCreateInput("", 5, 5, 290)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)    ;HiWord

    If $iIDFrom = $inTest And $iCode = $EN_CHANGE Then

    $Read_Input = GUICtrlRead($inTest)
    If StringRegExp($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]') Then $Read_Input = StringRegExpReplace($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]', '\1')
    $Point1 = StringInStr($Read_Input, ".", 0)
    $Point2 = StringInStr($Read_Input, ".", 0, 2)
    If $Point2 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point2 - 1)
   If $Point1 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point1 + $iDecimal)

;///////
If $Read_Input = "."    Then
    $Read_Input = 0
EndIf
;///////

GUICtrlSetData($inTest, $Read_Input)

    EndIf

EndFunc;==>_WM_COMMAND

Thanks

Hobbyist

Link to comment
Share on other sites

$ES_NUMBER doesn't allow minus/dot

Maybe this ?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

GUICreate("Input Filter", 300, 30, -1, -1)
Global $inTest = GUICtrlCreateInput("", 5, 5, 290)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)    ;HiWord

    If $iIDFrom = $inTest And $iCode = $EN_CHANGE Then
      $Read_Input = GUICtrlRead($inTest)
      If not StringRegExp($Read_Input, '^[\d-]?\d*(?:\.\d*)?$') Then 
            GUICtrlSetData($inTest, StringTrimRight($Read_Input, 1))
      Else
            GUICtrlSetData($inTest, StringRegExpReplace($Read_Input, '^(-?)\.', '${1}0.'))
      EndIf
    EndIf
EndFunc;==>_WM_COMMAND

 

Link to comment
Share on other sites

@mikell

 

Thanks for replying.

When I run your script, I get an error @ Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord

syntax error - illegal character.  But I cannot see why this is and therefor cannot see the results of your solution.  Am I overlooking something?

 

Hobbyist

Link to comment
Share on other sites

3 hours ago, Hobbyist said:

When I run your script, I get an error @ Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord

syntax error - illegal character.  But I cannot see why this is and therefor cannot see the results of your solution.  Am I overlooking something?

 

Hobbyist

For me this is a problem with the edge browser. It adds illegal whitespace characters. Use another browser.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

  • 2 weeks later...

@mikell

After running the new script, I have come upon an instance of conflict.

Everything works great with your script and it does all it is suppose to, but in the case of a "-" sign there are two possible outcomes. User presses -45 which is legit. What if the user just presses "-".  The script still takes it.   It is legit only when applied with a number. By itself, it could cause a calculation problem.

 

Thanks

Hobbyist

Link to comment
Share on other sites

@mikell

If StringRegExp(GuiCtrlRead($inTest), '\d') Then


 if  StringLen ((  GUICtrlRead($inTest) )) = 1  and  GUICtrlRead($inTest) = "-"   Then
  GUICtrlSetData($inTest, "")
   GUICtrlSetState($inTest, $GUI_FOCUS)
   ElseIf     StringLen ((  GUICtrlRead($inTest) )) > 1 Then
  $Read_Input = $inTest
  EndIf
  GUICtrlSetData($inTest,    GUICtrlRead($inTest))
 EndIf

This is what I thought you meant but guess not.

I think the inputbox needs to know "when" the typing of input if complete and then do the comparison. Am I partially right on this.

 

Thanks

Hobbyist

Link to comment
Share on other sites

The inputbox can't guess the precise moment when the typing is assumed to be complete  :)
The previous script is intended to check the typing while it is entered. The final comparison/check must be done when the input content is read, and the easiest way to do this is to check if the input contains at least one digit

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

GUICreate("Input Filter", 300, 60, -1, -1)
Global $inTest = GUICtrlCreateInput("", 5, 5, 290)
$btn = GUICtrlCreateButton("Validate", 200, 30, 60, 25)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)

While 1
 $msg = GUIGetMsg() 
 If $msg = $GUI_EVENT_CLOSE Then Exit
 If $msg = $btn Then
    $content = GuiCtrlRead($inTest)
    If  StringRegExp($content, '\d') Then 
         Msgbox(0,"", "OK, " & $content & " is valid")
    Else
         GUICtrlSetData($inTest, "")
         Msgbox(0,"", "Entry is invalid. Try again")
    EndIf
 EndIf
WEnd


Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)    ;HiWord
    If $iIDFrom = $inTest And $iCode = $EN_CHANGE Then
      $Read_Input = GUICtrlRead($inTest)
      If not StringRegExp($Read_Input, '^[\d-]?\d*(?:\.\d*)?$') Then 
            GUICtrlSetData($inTest, StringTrimRight($Read_Input, 1))
      Else
            GUICtrlSetData($inTest, StringRegExpReplace($Read_Input, '^(-?)\.', '${1}0.'))
      EndIf
    EndIf
EndFunc;==>_WM_COMMAND

Edit
Rectification : it could be done though by checking $EN_KILLFOCUS with WM_COMMAND (moment when the input loses focus) but it is hazardous, such a behavior could be baffling for the user ^^  :)

Edited by mikell
Link to comment
Share on other sites

If you want to test the $EN_KILLFOCUS thing by yourself anyway then do it like this  :)

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)    ;HiWord
    If $iIDFrom = $inTest Then 
          $Read_Input = GUICtrlRead($inTest)
          If $iCode = $EN_KILLFOCUS Then
             If StringRegExp($Read_Input, '^-$') Then GUICtrlSetData($inTest, "")
          EndIf
          If $iCode = $EN_CHANGE Then
             If not StringRegExp($Read_Input, '^[\d-]?\d*(?:\.\d*)?$') Then 
                  GUICtrlSetData($inTest, StringTrimRight($Read_Input, 1))
             Else
                  GUICtrlSetData($inTest, StringRegExpReplace($Read_Input, '^(-?)\.', '${1}0.'))
             EndIf
          EndIf
     EndIf
EndFunc;==>_WM_COMMAND

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...