Sign in to follow this  
Followers 0
Rigest

Checking an inputfields format

6 posts in this topic

Pretty much what the title says. I want to check if the data entered in an inputfield is a number or not. If it's not a number i want to get an error popup with this error. Right now i am using GUICtrlCreateInput to create this input, but reading this returns a string. So IsInt returns 0. Next thing i have tried is Int($input), hoping it would set @error to 1 when $input is not a number. Unfortunately, this isn't the case, it even returns 0 when it's not a number, so i can't apply the IsInt function. Does anyone have a solution to this problem?

Thanks in advance!

Share this post


Link to post
Share on other sites



You can use $ES_NUMBER style or StringRegExp($sString, '[^0-9]') - it will return 1 if $sString is containing a char, that isn't number.

Share this post


Link to post
Share on other sites

If Number(GUICtrlRead($input))=0 and GUICtrlRead($input)<>"0" Then MsgBox(0,"error","error")

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If Number(GUICtrlRead($input))=0 and GUICtrlRead($input)<>"0" Then MsgBox(0,"error","error")

If $input contains "00"?

Edited by dwerf

Share this post


Link to post
Share on other sites

Rigest,

A couple of ways to limit your inputs to integers only - where 0 is not considered an integer: ;)

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

$hGUI = GUICreate("Test", 500, 500)

; This Input uses the standard windows style
$hInput_1 = GUICtrlCreateInput("", 10, 10, 200, 20, $ES_NUMBER)

; This Input gets its $EN_CHANGE message intercepted
$hInput_2 = GUICtrlCreateInput("", 10, 110, 200, 20)

$hButton = GUICtrlCreateButton("Read", 10, 200, 80, 30)

GUISetState()

; This line only needed if you use the GUIRegisterMsg method
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            ; Read input
            $sRead_1 = GUICtrlRead($hInput_1)
            ; Check we have something in the input
            If $sRead_1 <> "" And Number($sRead_1) <> 0 Then
                ; See if it is an int (It will be, of course!)
                If IsInt(Number($sRead_1)) Then $fInteger_1 = True
            Else
                $fInteger_1 = False
            EndIf
            ; And now for Input_2
            $sRead_2 = GUICtrlRead($hInput_2)
            If $sRead_2 <> "" And Number($sRead_2) <> 0 Then
                If IsInt(Number($sRead_2)) Then $fInteger_2 = True
            Else
                $fInteger_2 = False
            EndIf
            ; Show the results
            MsgBox(0, "Results", "Input_1:  " & $fInteger_1 & @CRLF & "Input_2:  " & $fInteger_2)
    EndSwitch
WEnd

; This function only needed if you use the GUIRegisterMsg method
Func _WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $lParam

    If BitAND($wParam, 0xFFFF) = $hInput_2 And BitShift($wParam, 16) = 0x300 Then ; $EN_CHANGE
        Local $sInput = GUICtrlRead($hInput_2)
        If StringRegExp($sInput, '[^\d]') Then $sInput = StringRegExpReplace($sInput, '[^\d]', '')
        GUICtrlSetData($hInput_2, $sInput)
    EndIf

EndFunc   ;==>_WM_COMMAND

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Hello, I want to allow only digits and decimal point, so $ES_NUMBER isn't helpful.

Should I use a regular expression in my dialog loop to filter out other characters ? If yes, can someone quickly post the reg expression to use ?

EDIT: Nevermiiiind

Edited by Inverted

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