Jump to content

Data Validation for GUI Input


Recommended Posts

I have used a UDF before that could restrict the data type for an input, and I found this today by searching:

But the solution Melba has in that thread causes my input to refresh over and over so it won't work for this situation,  As for that UDF I have no idea where that old UDF is.  So figured maybe I should just ask as their may be new UDF or new methods that I am not aware of.  Basically I have a GUI that asks for the users Name/ID that submits to a database.

In production I am having people put a 4 digit number for their name instead of their real name or computer ID so I want to restrict the input to make input of letters mandatory and give a notification of sorts to the user when they try to type numbers so they know why they cant type numbers and what is required in the field. 

Link to comment
Share on other sites

  • Moderators

ViciousXUSMC,

That code makes me blush it is so inelegant  - I am sure we can do better for you.  But:

In production I am having people put a 4 digit number for their name instead of their real name or computer ID so I want to restrict the input to make input of letters mandatory and give a notification of sorts to the user when they try to type numbers

makes no sense to me. You start by speaking of entering a 4-digit number and then say that you want to limit the input to letters only. Just what do you want in this input?

M23 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

I am asking for Name or ID in the fields label.

So I am expecting their real name John Doe or Computer ID JohnDo both of those I know who the person is entering into the database.  But this is Fire Services and they are putting in their 911 ID used for CAD and that is a 4 digit number.

I have no way to cross reference that 4 digit number to who the person is, so I think the best approach here (since use training is not an option lol) is to restrict input to letters only and block numeric input. 

 

My wording may not have been clear but what I intended to say was that my code being used in production did not get desired results where people put their name or computer id and instead they are entering a 4 digit number.

Link to comment
Share on other sites

  • Moderators

ViciousXUSMC,

Try this and see what you think:

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

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

$cInput = GUICtrlCreateInput("", 10, 10, 200, 20)

GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd



Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam)

    ; If it was an update message from our input
    If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $cInput Then

        ; Read content
        $sContent = GUICtrlRead($cInput)
        ; check if any non-letters
        If StringRegExp($sContent, "[^A-Za-z]") Then
            ; Replace any non-letters
            $sContent = StringRegExpReplace($sContent, "[^A-Za-z]", "")
            ; Colour input
            GUICtrlSetBkColor($cInput, 0xFFCCCC)
            ; Create tootip

            $aPos = WinGetPos($hGUI)
            ToolTip("Letters only", $aPos[0] + 30, $aPos[1] + 50, "Error", $TIP_ERRORICON)
            ; Register function to clear tooltip and reset backcolour

            AdlibRegister("_ResetBkColor", 1000)
        EndIf

        ; Set the label to the new data
        GUICtrlSetData($cInput, $sContent)

    EndIf



EndFunc   ;==>_WM_COMMAND

Func _ResetBkColor()
    AdlibUnRegister("_ResetBkColor")
    GUICtrlSetBkColor($cInput, 0xFEFEFE)
    ToolTip("")
EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Maybe you can use GUIRegisterMsg to intercept the pressed keys, and then show and error message if needed.

One example :

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

GUICreate("GUI")

Global $sErrorText = "This caracter is not supported"
Global $sAllowedKeys = "[a-zA-Z]"
Global $iErrorDuration = 1000
Global $iError = 0, $hTimer = 0

Global $input = GUICtrlCreateInput("", 10, 10, 200, 20)
Global $errorLabel = GUICtrlCreateLabel("", 10, 30, 200, 20)
GUICtrlSetColor(-1, 0xff0000)

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    
    If $iError Then ShowError()
WEnd


Func ShowError()
    If $hTimer Then
        If TimerDiff($hTimer) >= $iErrorDuration Then
            $hTimer = 0
            GUICtrlSetData($errorlabel, "")
            $iError = 0
        endIf
    Else
        GUICtrlSetData($errorlabel, $sErrorText)
        $hTimer = TimerInit()
    EndIf
EndFunc


Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0x0000FFFF)
    Local $sInputContent

    Switch $nID
        Case $input
            Switch $nNotifyCode
                Case $EN_UPDATE
                    If NOT StringRegExp( GUICtrlRead($input), "^" & $sAllowedKeys & "*$") Then
                        $iError = 1
                        GUICtrlSetData($input, StringRegExpReplace( GUICtrlRead($input), StringRegExpReplace($sAllowedKeys, "^\[\K", "^"), "") )
                    EndIf
            EndSwitch
    EndSwitch
EndFunc

 

Edited by jguinch
Link to comment
Share on other sites

  • Moderators

ViciousXUSMC,

I'll try both [...] and give feedback

I can hardly wait....  To save you some time, the 2 scripts are essentially the same - looking for a change in input content and replacing any non-letter characters.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Melba - Yours works great

Was missing the constant for  $TIP_ERRORCORRECTION for some reason.  Helpfile says include AutoITConstants.au3 but that did not fix it either so I just changed that to a "3" to make the example work.

I found your example easy to integrate into my script and modify the tooltip for the proper position.

 

jguinch - Got yours working as well "character" typo for the label :)

 

In both cases I need to figure out how to modify the RegEX to allow spaces since First Last name needs a space between.  I am sure that is easy enough for me to figure out with some google.

Both great working examples and I appreciate the time and effort on both behalves. 

Edited by ViciousXUSMC
Link to comment
Share on other sites

ViciousXUSMC,

M23's code with another way to handle the invalids input message...

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

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

$cInput = GUICtrlCreateInput("", 10, 10, 200, 20)

GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch



WEnd







Func _WM_COMMAND($hWHnd, $iMsg, $wParam, $lParam)

    ; If it was an update message from our input
    If _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $cInput Then

        ; Read content
        $sContent = GUICtrlRead($cInput)
        ; check if any non-letters
        If StringRegExp($sContent, "[^A-Za-z]") Then
            ; Replace any non-letters
            $sContent = StringRegExpReplace($sContent, "[^A-Za-z]", "")
            ; Colour input
            GUICtrlSetBkColor($cInput, 0xFFCCCC)
            ; Create tootip



            _GUICtrlEdit_ShowBalloonTip($cInput,'Input Error','Valid chars are...',$TTI_ERROR) ; <---- another way to display error message

            AdlibRegister("_ResetBkColor", 1000)
        EndIf



        ; Set the label to the new data
        GUICtrlSetData($cInput, $sContent)

    EndIf







EndFunc   ;==>_WM_COMMAND

Func _ResetBkColor()
    AdlibUnRegister("_ResetBkColor")
    GUICtrlSetBkColor($cInput, 0xFEFEFE)
    ToolTip("")
EndFunc

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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