Jewtus

IsNumber issue on GUICtrlRead

3 posts in this topic

#include <GUIConstantsEx.au3> ; $gui_Disable

$GUIT_OAdd = GUICreate("Add", 182, 170, 192, 114)
$labField1 = GUICtrlCreateLabel("Year", 8, 8, 32, 17)
$idField1 = GUICtrlCreateInput("", 51, 3, 121, 21)
$labField2 = GUICtrlCreateLabel("Hours", 8, 32, 32, 17)
$idField2 = GUICtrlCreateEdit("", 51, 27, 121, 100)
$btnOK = GUICtrlCreateButton("OK", 144, 140, 27, 25)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete($GUIT_OAdd)
            ExitLoop
        Case $btnOK
            If IsNumber(GUICtrlRead($idField1))=0 OR IsNumber(GUICtrlRead($idField2))=0 Then
                MsgBox(0,"Oops","You need to enter integers for year and hours."&@TAB&GUICtrlRead($idField1))
                GUICtrlSetData($idField1,"")
                GUICtrlSetData($idField2,"")
            ElseIf StringLen(GUICtrlRead($idField1))>4 OR StringLen(GUICtrlRead($idField1))<4 Then
                MsgBox(0,"Oops","You need to enter the 4 digit year"&@TAB)
            Else
                Local $aReturn[1][2]=[[GUICtrlRead($idField1),GUICtrlRead($idField2)]]
                GUIDelete($GUIT_OAdd)
            EndIf
    EndSwitch
WEnd

I'm trying to use the above code to validate the entries to ensure that they are numeric. When I take a look at the value of GuiCtrlRead($idFIeld1) it is in fact a number, but IsNumber is saying that it isn't. Does any one know what I'm doing wrong here

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

An easy way is to use the $ES_NUMBER style for your inputs, so it prevent users to enter something else than numbers.

Edit : to answer to you question : 1 is a number, "1" is not a number, it is a string. GUICtrlRead returns a string. You can check the input to be 4 numbers with something like this :

If StringRegExp( GUICtrlRead($idField1), "^\d{4}$") AND StringRegExp( GUICtrlRead($idField2), "^\d{4}$") Then  ; OK

 

Edited by jguinch
1 person likes this

Share this post


Link to post
Share on other sites

Ahhh ok that makes sense. My stringLen function still works and when I use $ES_Number, it resolves the need to validate if it is a number. Thanks!

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