Sign in to follow this  
Followers 0
pete1234

Input box validation

5 posts in this topic

I have an input box and need to validate the data so it includes only numbers, and won't accept values such as 01, 02, etc. I have this so far

#include <GUIConstants.au3>
    
    Global $sInput
    Local $sIniReadCount
    $buGUI = GUICreate("How many backups?", 180, 100, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU))
    GUICtrlCreateLabel("Enter a number from 1 to 99", 10, 10)
    $buInputBox = GUICtrlCreateInput("", 10, 30, 160, Default, $ES_NUMBER)
    GUICtrlSetData($buInputBox, $sIniReadCount)
    $btOk = GUICtrlCreateButton("Ok", 10, 65, 75)
    GUICtrlSetState($btOk, $GUI_DISABLE)
    $buOkEnabled = False
    $buCancel = GUICtrlCreateButton("Cancel", 95, 65, 75)
    GUISetState()

    While 1
        $nMsg = GUIGetMsg()
        Global $sInputTemp = GUICtrlRead($buInputBox)
        Select
            Case $nMsg = $GUI_EVENT_CLOSE Or $nMsg = $buCancel
                Exit
            Case $nMsg = $btOk
                GUISetState(@SW_HIDE)
                ExitLoop
            Case StringLen($sInputTemp) > 2
                GUICtrlSetData($buInputBox, StringLeft($sInput, 2))
            Case StringLen($sInputTemp) = 2 And StringLeft($sInputTemp, 1) = "0"
                $sInput = StringRight($sInputTemp, 1)
            Case StringLen($sInputTemp) = 2 And Not $buOkEnabled
                GUICtrlSetState($btOk, $GUI_ENABLE)
                $buOkEnabled = True
                $sInput = $sInputTemp
                ExitLoop    
            Case StringLen($sInputTemp) = 1 And Not $buOkEnabled
                GUICtrlSetState($btOk, $GUI_ENABLE)
                $buOkEnabled = True
                $sInput = $sInputTemp
            Case StringLen($sInputTemp) = 0 And $buOkEnabled
                GUICtrlSetState($btOk, $GUI_DISABLE)
                $buOkEnabled = False
        EndSelect
    WEnd
    
    msgbox(0, "sinput", $sInput)

It strips away the leading zeroes just fine, but if the input is legitimately 2 digits, $sInput will only return the first digit. So I'm thinking there has to be something funky going on with StringLeft, but I can't figure it out. :/

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Global $sInputTemp = GUICtrlRead($buInputBox)

If StringLeft($sInputTemp, 1) = "0" Then GUICtrlSetData($buInputBox, StringMid($sInputTemp,2))

Edit: Ooooops, use this instead.

Global $sInputTemp = GUICtrlRead($buInputBox)
If StringLeft($sInputTemp, 1) = "0" AND StringLen($sInputTemp) >= 2 Then GUICtrlSetData($buInputBox, StringMid($sInputTemp,2))

Yiu could also use this to make sure that there are no more than 2 characters.

Global $sInputTemp = GUICtrlRead($buInputBox)
If StringLeft($sInputTemp, 1) = "0" AND StringLen($sInputTemp) >= 2 Then GUICtrlSetData($buInputBox, StringMid($sInputTemp,2, 2))
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Nice! I don't even have to use the temp variable anymore. Thanks.

Here's the code if anyone is interested

Local $sIniReadCount
    $buGUI = GUICreate("How many backups?", 180, 100, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU))
    GUICtrlCreateLabel("Enter a number from 1 to 99", 10, 10)
    $buInputBox = GUICtrlCreateInput("", 10, 30, 160, Default, $ES_NUMBER)
    GUICtrlSetData($buInputBox, $sIniReadCount)
    $btOk = GUICtrlCreateButton("Ok", 10, 65, 75)
    GUICtrlSetState($btOk, $GUI_DISABLE)
    $buOkEnabled = False
    $buCancel = GUICtrlCreateButton("Cancel", 95, 65, 75)
    GUISetState()

    While 1
        $nMsg = GUIGetMsg()
        Global $sInput = GUICtrlRead($buInputBox)
        If StringLeft($sInput, 1) = "0" AND StringLen($sInput) >= 2 Then GUICtrlSetData($buInputBox, StringMid($sInput,2))
        Select
            Case $nMsg = $GUI_EVENT_CLOSE Or $nMsg = $buCancel
                Exit
            Case $nMsg = $btOk
                GUISetState(@SW_HIDE)
                ExitLoop
            Case StringLen($sInput) > 2
                GUICtrlSetData($buInputBox, StringLeft($sInput, 2))
            Case StringLen($sInput) = 2 And Not $buOkEnabled
                GUICtrlSetState($btOk, $GUI_ENABLE)
                $buOkEnabled = True
            Case StringLen($sInput) = 1 And Not $buOkEnabled
                GUICtrlSetState($btOk, $GUI_ENABLE)
                $buOkEnabled = True
            Case StringLen($sInput) = 0 And $buOkEnabled
                GUICtrlSetState($btOk, $GUI_DISABLE)
                $buOkEnabled = False
        EndSelect
    WEnd
Edited by pete1234

Share this post


Link to post
Share on other sites

IMO you don't need all this stuff ... if you need to get rid of the leading 0 you can use

Number(GUICtrlRead($buInputBox))

that will strip any "0" in front of the number - even a "gazillion" muttley


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

enaiman

Good solution muttley

pete1234

Try this:

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

Global $sInput
Local $sIniReadCount

$buGUI = GUICreate("How many backups?", 180, 100, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU))

GUICtrlCreateLabel("Enter a number from 1 to 99", 10, 10)

$buInputBox = GUICtrlCreateInput("", 10, 30, 160, Default, $ES_NUMBER)
GUICtrlSetLimit(-1, 2)
GUICtrlSetData($buInputBox, $sIniReadCount)

$btOk = GUICtrlCreateButton("Ok", 10, 65, 75)

$buCancel = GUICtrlCreateButton("Cancel", 95, 65, 75)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE Or $nMsg = $buCancel
            Exit
        Case $nMsg = $btOk
            ExitLoop
    EndSelect
WEnd
    
MsgBox(0, "sinput", Number(GUICtrlRead($buInputBox)))

Share this post


Link to post
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
Sign in to follow this  
Followers 0