Jump to content

Input Mask


SaphuA
 Share

Recommended Posts

Use $ES_NUMBER style

Going deeper on this topic, I wonder if there is a way to check if the 3 first characters of the input allowing only numbers, and the rest alphanumeric. Currently I check the input when finished, I would like it while typing... with a beep if the character is invalid at that position, or a red background in the input field. That last, I have already seen in other programs.
Link to comment
Share on other sites

Going deeper on this topic, I wonder if there is a way to check if the 3 first characters of the input allowing only numbers, and the rest alphanumeric. Currently I check the input when finished, I would like it while typing... with a beep if the character is invalid at that position, or a red background in the input field. That last, I have already seen in other programs.

A while loop that cuts the first 3 characters using stringtrim out and checks to see if they are numbers, using isnumber. Look up beep in the help file and guictrlsetcolor and guictrlsetdefcolor and guictrlsetdefbkcolor.

Giggity

Link to comment
Share on other sites

A while loop that cuts the first 3 characters using stringtrim out and checks to see if they are numbers, using isnumber. Look up beep in the help file and guictrlsetcolor and guictrlsetdefcolor and guictrlsetdefbkcolor.

Your answer is what I have thought about, but can this be done WHILE typing? Then it must be a 1-character input at a time, checking the validity of each typed character.
Link to comment
Share on other sites

Going deeper on this topic, I wonder if there is a way to check if the 3 first characters of the input allowing only numbers, and the rest alphanumeric. Currently I check the input when finished, I would like it while typing... with a beep if the character is invalid at that position, or a red background in the input field. That last, I have already seen in other programs.

Here is an example of checking the changes in the input field:

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

Global $iTimer = -1
Global $iTime_Expired = 2000

GUICreate("Input Changed GUI", 300, 140)

GUICtrlCreateLabel("You can set only numbers as 3 first characters:", 20, 40)
$Input = GUICtrlCreateInput("", 20, 70, 260, 20)

$Exit = GUICtrlCreateButton("Exit", 20, 100, 60, 20)

GUISetState()
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $Exit
            ExitLoop
    EndSwitch
    
    If $iTimer <> -1 And TimerDiff($iTimer) >= $iTime_Expired Then
        $iTimer = -1
        GUICtrlSetBkColor($Input, 0xFFFFFF)
    EndIf
WEnd

Func WM_COMMAND($hWnd, $nMsg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0xFFFF)
    Local $hCtrl = $lParam
    
    Switch $nID
        Case $Input
            Switch $nNotifyCode
                Case $EN_UPDATE
                    Local $sRead_Input = GUICtrlRead($Input)
                    Local $sFirstChars = StringLeft($sRead_Input, 3)
                    Local $sLastChars = ""
                    If StringLen($sRead_Input) > 3 Then $sLastChars = StringMid($sRead_Input, 4)
                    
                    If Not StringIsDigit($sFirstChars) Then
                        ;Uncomment this line to set a "Beep Sound" on wrong set of characters
                        ;DllCall("User32.dll", "int", "MessageBeep", "int", -1)
                        $iTimer = TimerInit()
                        
                        GUICtrlSetBkColor($Input, 0xFFCACA)
                        GUICtrlSetData($Input, StringRegExpReplace($sFirstChars, "[^\d]+", "") & $sLastChars)
                    Else
                        $iTimer = -1
                        GUICtrlSetBkColor($Input, 0xFFFFFF)
                    EndIf
                Case $EN_SETFOCUS
                    
                Case $EN_KILLFOCUS
                    
            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Awesome! :)

MrCreatoR, thank you so much! This is exactly what I was dreaming of! I saw this kind of error-checking on the European Intrastat software, and the input background became red when an Intrastat code number does not exist. This occurred often because the codes are long. The 3-digits example was for my application.

The UDF you just wrote is so useful and professional that it should be included in the AutoIt's UDF Collection.

Link to comment
Share on other sites

  • Moderators

@charvi,

Strangely enough I was looking for very much the same thing a few weeks ago and could not find anything on the forums. So I developed a way of masking an input - I was looking to accept only hex (0-9 and A-F) as input to the RGB colour selector. I found $ES_NUMBER, $ES_UPPERCASE & $ES_LOWERCASE but no $ES_HEX!. The principle of my solution should be able to do what you want - all you have to do is define what you will accept at that point in the input.

As I said, the script is a RGB colour selector, but you should look closely at the _Check_Input() function. It checks the input box on a character by character basis and I believ it can be modified to meet your requirements without too much difficulty. I have extracted 2 important functions from GUIEdit.au3 to make the whole process clearer - of course, including the whole UDF simplifies matters for normal use. I hope the whole script is understandable without comments - if not please do not hesitate to ask:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <SendMessage.au3>
#include <Misc.au3>

Opt("GUICloseOnESC", 0)

Global $iCol_Begin = "0x000000"
Global $iCol_Chosen

; ========= Create GUI ==========

$hRGB_Win = GUICreate("RGB Colour Selection", 390, 110)

; Create RGB number label
$hCol_Number = GUICtrlCreateLabel ($iCol_Begin, 100, 20, 80, 20, $SS_CENTER)
GUICtrlSetFont(-1, 10)

; Create colour display
$hCol_Display = GUICtrlCreateLabel ("",200, 10, 80, 40)
GUICtrlSetBkColor($hCol_Display, $iCol_Begin)

; Create buttons
$hFrom_Clip =  GUICtrlCreateButton("From Clipboard", 10, 10, 80, 40)

$hFrom_Pixel = GUICtrlCreateButton("From Pixel",     10, 60, 80, 40)

$hFrom_Edit =  GUICtrlCreateButton("Enter RGB",  100, 60, 80, 40)

$hSelect_Col = GUICtrlCreateButton("Select",        200, 60, 80, 40)

$hTo_Clip = GUICtrlCreateButton("To Clipboard",  300, 10, 80, 40)

$hExit_RGB =   GUICtrlCreateButton("Exit",        300, 60, 80, 40)

; Display the GUI
GUISetState(@SW_SHOW, $hRGB_Win)

; Create the edit window
$hEdit_Win = GuiCreate("", 70, 20, 105, 19, $WS_POPUP, $WS_EX_MDICHILD, $hRGB_Win)
GUISetBkColor(0xffff00)
    
$hEdit_Edit  = GUICtrlCreateEdit("", 0, 0, 70, 20, $ES_UPPERCASE)
GUICtrlSetFont(-1, 10)
GUICtrlSetLimit(-1, 6)
GUICtrlSetState(-1, $GUI_FOCUS)

GUISetState(@SW_HIDE, $hEdit_Win)

; ======== Waiting Loop =========

While 1
    
    $iMsg = GUIGetMsg()
    
    Switch $iMsg
        Case $GUI_EVENT_CLOSE, $hExit_RGB
            On_Exit()
        Case $hSelect_Col
            On_Select()
        Case $hFrom_Clip
            From_Clip()
        Case $hFrom_Pixel
            From_Pixel()
        Case $hFrom_Edit
            From_Edit()
        Case $hTo_Clip
            To_Clip()
    EndSwitch
   
WEnd

; ========== Functions ==========

Func On_Select()

    $iCol_Chosen = _ChooseColor(2, $iCol_Begin, 2, $hRGB_Win)
    If $iCol_Chosen = -1 Then $iCol_Chosen = "0x000000"
    
    Col_Set($iCol_Chosen)

EndFunc

; ----------

Func From_Clip()

    $iCol_Input = ClipGet()
    
    $iCol_Begin = 0x000000

    If IsNumber(Dec($iCol_Input)) Then
        If Dec($iCol_Input) >= 0 And Dec($iCol_Input) <= 0xFFFFFF Then
            $iCol_Begin = "0x" & Hex($iCol_Input, 6)
        EndIf
    EndIf

    Col_Set($iCol_Begin)
        
EndFunc

; ----------

Func From_Pixel()

    Local $aMouse_Pos[2]

    $hDll = DllOpen("user32.dll")

    While 1

        If _IsPressed("01") = 1 Then
            $aMouse_Pos = MouseGetPos()
            ExitLoop
        EndIf
        
    Wend

    DllClose($hDll)

    $iCol_Begin = "0x" & Hex(PixelGetColor($aMouse_Pos[0], $aMouse_Pos[1]), 6)
    
    WinActivate($hRGB_Win, "")
    
    Col_Set($iCol_Begin)

EndFunc

; ----------

Func From_Edit()

    GUISetState(@SW_SHOW, $hEdit_Win)
    GUISetState(@SW_DISABLE, $hRGB_Win)

    $hDll = DllOpen("user32.dll")

    While 1

        If _GUICtrlEdit_GetModify($hEdit_Edit) = True Then

            If _Check_Input($hEdit_Edit) = 1 Then
                $iCol_Begin = "0x" & GUICtrlRead($hEdit_Edit)
                Col_Set($iCol_Begin)
                ExitLoop
            EndIf
        EndIf
        
        If _IsPressed("1B") = 1 Then
            ExitLoop
        EndIf
        
    Wend
    
    DllClose($hDll)
    GUICtrlSetData($hEdit_Edit, "")
    GUISetState(@SW_ENABLE, $hRGB_Win)
    GUISetState(@SW_HIDE, $hEdit_Win)
;WinActivate($hRGB_Win, "")

EndFunc

; -----

Func _Check_Input($hEdit)

    $sText = GUICtrlRead($hEdit)
    $sLast_Char = StringRight($sText, 1)
    GUICtrlSetBkColor($hEdit, 0xffffff)
    
    Switch Asc($sLast_Char)
        Case 48 To 59, 65 To 70
        ; Hex character
            If StringLen($sText) = 6 Then
                _GUICtrlEdit_SetModify($hEdit, False)
                Return 1
            EndIf
        Case Else
        ; Remove it
            GUICtrlSetData($hEdit, StringTrimRight($sText, 1))
            GUICtrlSetBkColor($hEdit, 0xffaaaa)
    EndSwitch
        
    _GUICtrlEdit_SetModify($hEdit, False)
    Return 0

EndFunc


; ----------

Func To_Clip()

    ClipPut($iCol_Chosen)

EndFunc

; ----------

Func Col_Set($iCol)

    GUICtrlSetData($hCol_Number, $iCol)
    GUICtrlSetBkColor($hCol_Display, $iCol)

EndFunc

; ----------

Func On_Exit()
    
    GUIDelete()
    
    Exit
    
EndFunc

; # FUNCTIONS from GUIEdit.au3 # ================================================================================================
; Name...........: _GUICtrlEdit_SetModify & _GUICtrlEdit_GetModify
; Author ........: Gary Frost (gafrost)
; ===============================================================================================
Func _GUICtrlEdit_SetModify($hWnd, $fModified)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    _SendMessage($hWnd, $EM_SETMODIFY, $fModified)
EndFunc  ;==>_GUICtrlEdit_SetModify

Func _GUICtrlEdit_GetModify($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    Return _SendMessage($hWnd, $EM_GETMODIFY) <> 0
EndFunc  ;==>_GUICtrlEdit_GetModify

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

@Melba23,

This is a very interesting program, and the _Check_Input function is worth 5 stars too. It is true that a Hexadecimal value accepts only digits and letters from A to F. Any other character is invalid.

This has lead me to an idea. In your function you are trimming the last character if invalid, so I came to the idea to add a new one as replacement. I am too tired now, but tomorrow I will test it. What I will try is the following:--

I have a User and Password input. In my application the User is a number from 001 to 999. To avoid two inputs (the user needs to hit the Tab key to jump) I have only one input that contains user and password together. Actually, the whole input is simply an input with password flag ($ES_PASSWORD, showing big dots instead of what the user types).

Now that I can verify the input one by one, the first 3 characters need to be digits, if not then change to red background.... The user number will now be visible.

As from the 4th character, the input is the real password. No red background because we never check a password prematurely, but they can be displayed with big dots...

The only thing that I still miss is a hyphen (-) between the two zones...

Thanks for sharing!

Charvi

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