Jump to content

Inputbox allowing numbers only


Recommended Posts

Hallo everybody,

I'm having a little bit of a struggle with an array that I want to fill 4 with numbers. Basically, I want to loop-force the user to enter a valid number (and not some letter or stuff like that).

Since I only ever need 4 numbers, I intended to just create 4 simple "while"-loops

I've tried a few approaches for now, like

While ($fa[0] = "" OR NOT (isNumber($fa[0])))
    $fa[0] = InputBox("Number 1", "Max Amount", 0)
WEnd
While ($fa[0] = "" AND NOT(isNumber($fa[0])))
    $fa[0] = InputBox("Number 1", "Max Amount", 0)
WEnd

 

As well some other really abominal implementations. Sadly, AutoIt doesn't really seem to like the approach, so I keep getting stuck in the first loop.

I assume it might have something to do with the way arrays work or something, but I kind of run out of ideas, so any suggestions would be very nice.

 

Thanks!

Link to comment
Share on other sites

You can do it this way (one possible solution):

#include <Array.au3>

Dim $fa[4]
For $i = 0 To 3
    Do
        $fa[$i] = InputBox("Number "&$i, "Max Amount", 0)
    Until Int($fa[$i])>0
Next
_ArrayDisplay($fa)

or when number must have min and max value:

#include <Array.au3>

Dim $fa[4][3]   ;0 = value, 1=min, 2=max
$fa[0][1]=5
$fa[1][1]=6
$fa[2][1]=7
$fa[3][1]=8
$fa[0][2]=15
$fa[1][2]=16
$fa[2][2]=17
$fa[3][2]=18


For $i = 0 To 3
    Do
        $fa[$i][0] = InputBox("Number "&$i, "Max Amount", $fa[$i][1])
    Until Int($fa[$i][0])>=$fa[$i][1] And Int($fa[$i][0])<=$fa[$i][2]
Next
_ArrayDisplay($fa)

 

Link to comment
Share on other sites

IGNORE THIS!!!!!!

 

#include <Array.au3>

Dim $fa[4][3]   ;0 = value, 1=min, 2=max
$fa[0][1]=5
$fa[1][1]=6
$fa[2][1]=7
$fa[3][1]=8
$fa[0][2]=15
$fa[1][2]=16
$fa[2][2]=17
$fa[3][2]=18


For $i = 0 To 3
    Do
        $fa[$i][0] = InputBox("Number "&$i, "Max Amount", $fa[$i][1], $ES_NUMBER)
    Until Int($fa[$i][0])>=$fa[$i][1] And Int($fa[$i][0])<=$fa[$i][2]
Next
_ArrayDisplay($fa)

Add this to the InputBox, next time use the search function or a simple google search ;)
$ES_NUMBER

 

 

Edited by RyukShini
NOT USEABLE WITH INPUTBOX
Link to comment
Share on other sites

First of all, thanks for the suggestions. Sadly, they don't run as intended yet.

Apparently, there is a problem with the number 0. Both your solutions work perfectly fine for any positive number, however if I for example try to say

 

Until Int($fa[$i])>=0

or 

Until Int($fa[$i])>-1

the loop begins to accept letters again. 

Link to comment
Share on other sites

There is a valid reson on this earth why don't use a GUI? Who use Inputbox?

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

$Form1 = GUICreate("Name", 251, 161, -1, -1, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX))
$label1 = GUICtrlCreateLabel("Whatever you want to write", 10, 10)
$Input1 = GUICtrlCreateInput("", 24, 96, 201, 21, $ES_NUMBER)
GUICtrlSetLimit(-1, 4)
$Button1 = GUICtrlCreateButton("OK", 24, 128, 75, 25)
$Button2 = GUICtrlCreateButton("Cancel", 152, 130, 75, 25)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If StringLen(GUICtrlRead($Input1)) < 4 Then
                GUICtrlSetData($Input1, "")
            Else
                ConsoleWrite("Success" & @CRLF)
            EndIf
    EndSwitch
WEnd

Has a limitation of 4 numbers, is limited to numbers only and you can add whatever rules you want! Don't lose time with InputBox.

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

30 minutes ago, Bridge said:

Apparently, there is a problem with the number 0. Both your solutions work perfectly fine for any positive number, however if I for example try to say

Use Number instead of Int:

#include <Array.au3>

Dim $fa[4][3]   ;0 = value, 1=min, 2=max
$fa[0][1]=5
$fa[1][1]=6
$fa[2][1]=7
$fa[3][1]=-8
$fa[0][2]=15
$fa[1][2]=16
$fa[2][2]=17
$fa[3][2]=-2


For $i = 0 To 3
    Do
        $fa[$i][0] = InputBox("Number "&$i, "Max Amount", $fa[$i][1])
    Until Number($fa[$i][0])>=$fa[$i][1] And Number($fa[$i][0])<=$fa[$i][2]
Next
_ArrayDisplay($fa)

or better make your own InputBoxEX func using a GUI and GuiCtrlCreateInput.

Link to comment
Share on other sites

One of the core issues I imagine is that InputBox much like MsgBox is a blocking function.  So even if you had code that would correctly do what you wanted, while the box is spawned all other code execution is halted.

Terenz solution is clean, but not sure if you need negative numbers like you mentioned above, if so it wont work.

If you go with GUI you can use the same solution I posted yesterday for somebody that wanted numbers and period only for an IP address.:

 

To do 4 numbers only and negative I would try something like this ($Edit1)

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

$Form1 = GUICreate("Test GUI", 618, 276, 192, 124)
$Label1 = GUICtrlCreateLabel("Test GUI", 168, 24, 47, 17)
$Button1 = GUICtrlCreateButton("Toggle Validation", 290, 24, 110, 40)
$Input1 = GUICtrlCreateInput("Max 10 Characters", 40, 72, 553, 21)
$Input2 = GUICtrlCreateInput("Numb3rs 0nly", 40, 97, 553, 21)
$Edit1 = GUICtrlCreateEdit("Max 4 Numbers Only + Allow Negative", 40, 120, 553, 105)
GUISetState(@SW_SHOW)

$iToggle = 1

GUIRegisterMsg($WM_COMMAND, '_WM_COMMAND')

While 1
    Sleep(10)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $iToggle = $iToggle + 1
    EndSwitch
WEnd

Func _GUIRegExValidate($sInputLabel, $sRegExCapture, $sRegExReplace)
    $Step1Read = GUICtrlRead($sInputLabel)
    $Step2Validate = StringRegExpReplace($Step1Read, $sRegExCapture, $sRegExReplace)
    If @Extended = 0 Then Return
    GUICtrlSetData($sInputLabel, $Step2Validate)
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
If Mod($iToggle, 2) = 0 Then
;Example 1 Limit to 10 Characters
 _GUIRegExValidate($Input1, "(.{10})(.)", "$1")
;Example 2 Can only type numbers
 _GUIRegExValidate($Input2, "[^\d]", "")
;Example 3 Combind More Than One On Same Input limit to 10 characters, limit to numbers only, line break @ 10 characters to new line
 _GUIRegExValidate($Edit1, "[^\d-]", "") ;only digit or negative
 _GUIRegExValidate($Edit1, "(-.{4}|[^-].{3}).", "$1") ;only 4 digits or 5 if negative

 EndIf
EndFunc

Im not really good at regex so I bet there is a way to make it even better (like restrict "-" to the first character only, but since its just example/ideas I can let  you take it from here. 

Edited by ViciousXUSMC
Link to comment
Share on other sites

Instead of you using an Input or Inputbox why not use the function _GUICtrlIpAddress_Create and do it in an IP address control? That would be the easiest way to make sure the result is what you want.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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