Hobbyist Posted May 14, 2017 Share Posted May 14, 2017 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 ( -.) . expandcollapse popup#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 More sharing options...
benners Posted May 14, 2017 Share Posted May 14, 2017 just need the $ES_NUMBER style #include <GUIConstantsEx.au3> #include <EditConstants.au3> GUICreate("Input Filter", 300, 30, -1, -1) Global $inTest = GUICtrlCreateInput("", 5, 5, 290, -1, $ES_NUMBER) GUISetState(@SW_SHOW) While GUIGetMsg() <> $GUI_EVENT_CLOSE sleep(20) WEnd Link to comment Share on other sites More sharing options...
mikell Posted May 14, 2017 Share Posted May 14, 2017 $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 Hobbyist 1 Link to comment Share on other sites More sharing options...
Hobbyist Posted May 15, 2017 Author Share Posted May 15, 2017 @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 More sharing options...
funkey Posted May 15, 2017 Share Posted May 15, 2017 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 tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
mikell Posted May 15, 2017 Share Posted May 15, 2017 Probably unwanted 'non-break space' characters You might try this code on your script $newcontent = StringRegExpReplace($content,"\xa0"," ") or replace all spaces manually, or change browser as funkey said Link to comment Share on other sites More sharing options...
Hobbyist Posted May 15, 2017 Author Share Posted May 15, 2017 @funkey @mikell Thanks to both of you. It was indeed the browser. I switched to IE and did a copy and paste and everything was fine. I had no idea Edge was an issue. Link to comment Share on other sites More sharing options...
Hobbyist Posted May 27, 2017 Author Share Posted May 27, 2017 @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 More sharing options...
mikell Posted May 27, 2017 Share Posted May 27, 2017 This is usually done at the next step, where the input is checked if non-empty before validation , etc You might check if the input contains at least one digit If StringRegExp(GuiCtrlRead($inTest), '\d') Then .... Link to comment Share on other sites More sharing options...
Hobbyist Posted May 27, 2017 Author Share Posted May 27, 2017 @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 More sharing options...
mikell Posted May 27, 2017 Share Posted May 27, 2017 (edited) 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 expandcollapse popup#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 May 27, 2017 by mikell Hobbyist 1 Link to comment Share on other sites More sharing options...
Hobbyist Posted May 28, 2017 Author Share Posted May 28, 2017 @mikell This helps me very much. I see I was confusing myself. Enjoy your weekend. Thanks Hobbyist Link to comment Share on other sites More sharing options...
mikell Posted May 28, 2017 Share Posted May 28, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now