Rigest Posted October 2, 2010 Posted October 2, 2010 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!
dwerf Posted October 2, 2010 Posted October 2, 2010 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.
mage123 Posted October 2, 2010 Posted October 2, 2010 If Number(GUICtrlRead($input))=0 and GUICtrlRead($input)<>"0" Then MsgBox(0,"error","error")
dwerf Posted October 2, 2010 Posted October 2, 2010 (edited) If Number(GUICtrlRead($input))=0 and GUICtrlRead($input)<>"0" Then MsgBox(0,"error","error")If $input contains "00"? Edited October 2, 2010 by dwerf
Moderators Melba23 Posted October 3, 2010 Moderators Posted October 3, 2010 Rigest, A couple of ways to limit your inputs to integers only - where 0 is not considered an integer: expandcollapse popup#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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Inverted Posted October 10, 2010 Posted October 10, 2010 (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 October 10, 2010 by Inverted
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now