Jump to content

I want to limit my input to only accept numbers, How?


Recommended Posts

Hey guys,

As the title and description of it both says:

I want to limit my input to only accept numbers, How??

Don't think of $ES_NUMBER because it's does not accept minus and dot!!

You see, "-0.865" is also a number, but $ES_NUMBER can not accept neither the minus "-" nor the dot "."

So, I need like a way to set my own restrictions to an input. Is there such a way?

If you ask me why do I need this, well it's for making kinda like a public Calculator.

And you know that Calculator does not expect a space or a letter in the as the inputs.

You might say, then why dont just use your pc regular calculator?

Well, my tool is kinda not just a calculator, you can say that the calculator I want to make will just a function in my script.

So...

Any suggestions, please? :D

[quote]#include <AutoIt.au3> [indent]$Nothing = Impossible[/quote] [/indent]

Link to comment
Share on other sites

I've thought of this just now.

I can use this function inside the button Case for starting calculation to just eliminate the spaces.

And maybe inside the While to do it all the time.

Here is what I've just made:

Func Temp()
    $EA = GUICtrlRead($Input1_EA)
    $Split = StringSplit($EA, " ")
    
    If $Split[0] = 1 Then
        GUICtrlSetData($Input1_EA, $Split[1])
    ElseIf $Split[0] = 2 Then
        GUICtrlSetData($Input1_EA, $Split[1] & $Split[2] )
    ElseIf $Split[0] = 3 Then
        GUICtrlSetData($Input1_EA, $Split[1] & $Split[2] & $Split[3] )
    ElseIf $Split[0] = 4 Then
        GUICtrlSetData($Input1_EA, $Split[1] & $Split[2] & $Split[3] & $Split[4] )
    ElseIf $Split[0] = 5 Then
        GUICtrlSetData($Input1_EA, $Split[1] & $Split[2] & $Split[3] & $Split[4] & $Split[5] )
    ElseIf $Split[0] = 6 Then
        GUICtrlSetData($Input1_EA, $Split[1] & $Split[2] & $Split[3] & $Split[4] & $Split[5] & $Split[6] )
    ElseIf $Split[0] = 7 Then
        GUICtrlSetData($Input1_EA, $Split[1] & $Split[2] & $Split[3] & $Split[4] & $Split[5] & $Split[6] & $Split[7] )
    EndIf
    
EndFunc

Beautiful function, isn't it? :P

But won't this take too much from the pc memory, process or anything like that?

I would really like to know if there is an easier alternative way than that long function. :o

Eliminating the spaces is my first target from this thread.

I don't think that the public who will use my script would be stupid enough to calculate characters like:

"abc + #%s = ?? " :D

But I still want it to be like the calculator in everybody's pc.

If you try to type a space or a character in the input then nothing should appear. :D

[quote]#include <AutoIt.au3> [indent]$Nothing = Impossible[/quote] [/indent]

Link to comment
Share on other sites

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Well, following code will transform the input into the respective numeric representation when the input loses focus. Invalid input will be transformed to 0. Maybe this helps you.

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

GUICreate("My GUI"); will create a dialog box that when displayed is centered

$c_input_a = GUICtrlCreateInput("",20,20)
$c_input_b = GUICtrlCreateInput("",20,80)

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW); will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    
WEnd
GUIDelete()

Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16);HiWord
    Switch $iIDFrom
        Case $c_input_a
            Switch $iCode
                Case 512
                    GUICtrlSetData($c_input_a,number(gUICtrlRead($c_input_a)))
            EndSwitch
    EndSwitch
EndFunc
Link to comment
Share on other sites

OHH YEAH!!

This is exactly what I'm looking for!! :o

Thanx for linking me to that thread. You've really saved me today. :D

But....

Could you or anyone here please explain me how the second parameter in this function should be typed?

I've seen the example test in there but I still need to know what each brackets and letters means in this second parameter:

This is the example included with that script:

#include <GUIConstants.au3>
#include "RestrictControlRegExp.au3"

Opt("GUIOnEventMode", 1)

_RegEx_RestrictControl_setup (3); prepare for up to 20 Controls to restrict

GUICreate("Test")
GUISetOnEvent(-3, "_quit")
$inp = GUICtrlCreateInput("", 10, 10, 100, 20)
_RegEx_RestrictControl_add ($inp, "^[a-z]{0,10}$"); up to 10 letters
$inp2 = GUICtrlCreateInput("", 10, 100, 100, 20)
_RegEx_RestrictControl_add ($inp2, "^[0123]{1}[0-9]{1}.[01]{1}[0-9]{1}.[12]{1}[0-9]{3}$", "12.12.1222"); German date DD.MM.YYYY
$inp3 = GUICtrlCreateInput("", 10, 200, 100, 20)
_RegEx_RestrictControl_add ($inp3, "^[a-zA-Z_0-9]{1,20}@[a-zA-Z_0-9]{2,20}.[a-z]{2,4}$", "g@gm.de"); e-mail-address


GUISetState()


While 1
    Sleep(10)
WEnd



Func _quit()
    Exit
EndFunc  ;==>_quit

-------------------------------------------------------------------------------------

Well, following code will transform the input into the respective numeric representation when the input loses focus. Invalid input will be transformed to 0. Maybe this helps you.

#include <GUIConstantsEx.au3>
 #include <WindowsConstants.au3>
 
 GUICreate("My GUI"); will create a dialog box that when displayed is centered
 
 $c_input_a = GUICtrlCreateInput("",20,20)
 $c_input_b = GUICtrlCreateInput("",20,80)
 
 GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
 GUISetState(@SW_SHOW); will display an empty dialog box
 
; Run the GUI until the dialog is closed
 While 1
     $msg = GUIGetMsg()
 
     If $msg = $GUI_EVENT_CLOSE Then ExitLoop
     
 WEnd
 GUIDelete()
 
 Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
     Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
     Local $iCode = BitShift($wParam, 16);HiWord
     Switch $iIDFrom
         Case $c_input_a
             Switch $iCode
                 Case 512
                     GUICtrlSetData($c_input_a,number(gUICtrlRead($c_input_a)))
             EndSwitch
     EndSwitch
 EndFunc
Uhm... I need to the restriction to be working as the users type in the input.

I'm not sure about what you mean by "transform the input into the respective numeric representation when the input loses focus" (very complex sentence for me to understand :D )

But about the part, " Invalid input will be transformed to 0" is exactly why I want to restrict the input while typing. I don't want it to be invalid in the first place.

Thanx for your reply, anyway. :P

[quote]#include <AutoIt.au3> [indent]$Nothing = Impossible[/quote] [/indent]

Link to comment
Share on other sites

I'm not sure about what you mean by "transform the input into the respective numeric representation when the input loses focus" (very complex sentence for me to understand :D )

Hmm, I'm feared for my complex sentences in German too :o...

Type something in the first input and then click on the second, you'll see what I meant.

Link to comment
Share on other sites

Hmm, I'm feared for my complex sentences in German too :o ...

Type something in the first input and then click on the second, you'll see what I meant.

Aaah, I see it now ^^

But, what are those parameters ($hWnd, $Msg, $wParam, $lParam)?

And how come that you're not using MY_WM_COMMAND() inside the While?

Is it because of this GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")?

And what does this GUIRegisterMsg mean anyway? (the help files in autoit explains in a complicated way)

Sorry for my lots of questions but I'm not familiar with such very deep codes . :D

[quote]#include <AutoIt.au3> [indent]$Nothing = Impossible[/quote] [/indent]

Link to comment
Share on other sites

No problem, hell, i took me a real long time to (partially) understand this.

Windows is a nice OS (at least in some ways :D). The GUIs created with au3 rely on APIs Window exposes to the enduser, so mainly the whole creation and display stuff is performed by windows in truth, au3 is only a method of accessing these APIs. One of the good functions Microsoft thought about was the internal messaging system utilizing Windows Message IDs.

With the GUIRegisterMsg() you hook up to this API and checked the Windows OS message about the GUI. The function is not in the while loop, because using GUIRegisterMsg() the function kicks in as soon as Windows broadcasts a message. The ($hWnd, $Msg, $wParam, $lParam) are standard parameters which Windows broadcast. The GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND") now defines that I want all Messages of a certain type (WM_COMMAND is control related (?), just using it and guessing here :D) to be routed to my special function.

And to be honest, most of this I do not understand to the deepest level myself :o, but it works!

Edit: And yeah, I'm glad about any clarification / correction on this topic :P...

Edited by KaFu
Link to comment
Share on other sites

No problem, hell, i took me a real long time to (partially) understand this.

Windows is a nice OS (at least in some ways :o ). The GUIs created with au3 rely on APIs Window exposes to the enduser, so mainly the whole creation and display stuff is performed by windows in truth, au3 is only a method of accessing these APIs. One of the good functions Microsoft thought about was the internal messaging system utilizing Windows Message IDs.

With the GUIRegisterMsg() you hook up to this API and checked the Windows OS message about the GUI. The function is not in the while loop, because using GUIRegisterMsg() the function kicks in as soon as Windows broadcasts a message. The ($hWnd, $Msg, $wParam, $lParam) are standard parameters which Windows broadcast. The GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND") now defines that I want all Messages of a certain type (WM_COMMAND is control related (?), just using it and guessing here :P ) to be routed to my special function.

And to be honest, most of this I do not understand to the deepest level myself :D , but it works!

Edit: And yeah, I'm glad about any clarification / correction on this topic :( ...

Wow, I never knew about that till now. ^^

Thanx for the explanation and after much comparing between your method and MrCreator method, I think that I will go with yours, KaFu. :P

The problem with the script Mrcreator showed me is that I can't paste into the input and can't backspace the first character after you've type it. Which is kinda annoying.

Still, there is a little bug in your method, KaFu. When I type a space in the middle of the value in the input, it then deletes the part after the space and accept only the part before the space.

What I want is that it kinda omit the space. And is there anyway that it can be inside the loop so that it does the job while typing?

Because I really don't want it to return 0 if the value is not a number.

And Btw, could you explain me what are those two line doing exactly:

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

What $wParam exactly is?

And I've always tried to understand what those Bit.... commands does but couldn't. :D

Edited by Derak

[quote]#include <AutoIt.au3> [indent]$Nothing = Impossible[/quote] [/indent]

Link to comment
Share on other sites

Just looked at example MrCreatoR proposed and saw it utilizes the same mechanism.

Change the ICode to 768 will capture input while typing.

The replacement can for sure be done in a single RegEx, but I'm no expert in that :D

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

GUICreate("My GUI"); will create a dialog box that when displayed is centered

$c_input_a = GUICtrlCreateInput("",20,20)
$c_input_b = GUICtrlCreateInput("",20,80)

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW); will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    
WEnd
GUIDelete()

Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16);HiWord
    Switch $iIDFrom
        Case $c_input_a
        ;ConsoleWrite($iIDFrom & @tab & $iCode & @crlf)
            Switch $iCode
                Case 768
                    $c_input_a_value = guictrlread($c_input_a)
                    $c_input_a_value = StringRegExpReplace($c_input_a_value,"[^-\.\d]","")
                    if StringInStr($c_input_a_value,".",0,2) then $c_input_a_value = StringReplace($c_input_a_value,".","",1)
                    if StringInStr($c_input_a_value,"-") then $c_input_a_value = "-" & stringReplace($c_input_a_value,"-","")
                    GUICtrlSetData($c_input_a,$c_input_a_value)
            EndSwitch
    EndSwitch
EndFunc

Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord

Local $iCode = BitShift($wParam, 16);HiWord

transform the parameters handed to the functions into a) the control ID and :o an action code. Why? Don't ask me :D...

Link to comment
Share on other sites

Very nice.

Thanx for your time and help, KaFu. :D

Now I can continue making my script thanx to your codes. ^^

[quote]#include <AutoIt.au3> [indent]$Nothing = Impossible[/quote] [/indent]

Link to comment
Share on other sites

  • 5 months later...

I was looking for this kind of script when I found this topic a few days(or maybe a week) ago... as an alternative to peethebee's UDF, this is my approach to this problem...

StringRegExpReplace($string,"["&StringRegExpReplace($string,"[\d.*/+-]","")&"]","")

..or in 3 lines...

$restrict = "\d.*/+-"   ; restrict input to numerals (0-9),"*","/","+" and "-"
$offset = StringRegExpReplace($string,"["&$restrict&"]","") ; trim $restrict from $string
$restricted = StringRegExpReplace($string,"["&$offset&"]","")   ; trim $offset from $string

I use double StringRegExpReplace instead of single StringRegExp for the simple reason that StringRegExp return an Array, StringRegExpReplace return a string... that's why.. theoretically, both will return the same result

Example 1:

$string = "ddasd adasd 96 ada / dasda 2 dads+sds3.5sd-0"
$restrict = "\d.*/+-"   ; restrict input to numerals (0-9),"*","/","+" and "-"
$offset = StringRegExpReplace($string,"["&$restrict&"]","") ; trim $restrict from $string
$restricted = StringRegExpReplace($string,"["&$offset&"]","")   ; trim $offset from $string
MsgBox(0,"",$restricted)

Example 2 (Simple Calculator):

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

$calcWin = GUICreate("Simple Calculator", 315, 144, 344, 178)
$input1 = GUICtrlCreateInput("", 40, 24, 121, 21)
$input2 = GUICtrlCreateInput("", 40, 72, 121, 21)
GUISetState(@SW_SHOW)


$restrict = "\d.*/+-"

$init = TimerInit()
$b = GUICtrlRead($input1)

While 1
    $msg = GUIGetMsg()
    $a = GUICtrlRead($input1)
    If $a <> $b Then
        $c = StringRegExpReplace($a,"["&$restrict&"]","")
        If $c <> "" Then GUICtrlSetData ($input1, StringRegExpReplace($a,"["&$c&"]",""))
        GUICtrlSetData ($input2, Execute(StringRegExpReplace($a,"["&StringRegExpReplace($a,"["&$restrict&"]","")&"]","")))
    EndIf
    If TimerDiff($init) > 200 Then
            $init = TimerInit()
            $b = GUICtrlRead($input1)
    EndIf
    If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd

I hope it's worth trying...

Hi ;)

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...