Jump to content

[SOLVED] Inputbox Rules


Recommended Posts

Hi guys,

I have searched into the forum without find an answer.

I want ot set to an inputbox rules like lenght and most important don't accept some special letter like | or &

I have do this:

$input=GUICtrlRead($Inputbox)
If $input="" Then
MsgBox(16,"Error","Input is empty")
Return
EndIf

$input=GUICtrlRead($Inputbox)
If $input = '<|&">' Then
MsgBox(16,"Error","Special character not allowed:" & @CRLF & '< | & " >')
Return
EndIf

The first work fine, but the second give me error for all inputbox. Some advice?

Thanks

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

To set the maximum number of characters in an input - look at GUICtrlSetLimit. ;)

To prevent certain characters is a little more complicated, but not at all impossible using the EN_CHANGE message. What characters to want to accept/exclude? :)

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 have write it on first post :) i want esclude this:

&

|

>

<

"

Thanks Melba for support

People can write on inputbox, but if they click OK i need an error message, then i add a GUICtrlSetData($Input,"")

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmacloud,

You will have to write your own GUI with an input if you want to use these tricks. An InputBox is a self-contained function and difficult to modify. Hang on a bit and I will see what I can do for you. :)

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

  • Moderators

johnmcloud,

Here is a short example of how you might do it: ;)

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

Global $hInput

$sInput = _InputBox("Title", "Prompt")

MsgBox(0, "Input", $sInput)

Func _InputBox($sTitle, $sPrompt)

    $hGUI = GUICreate($sTitle, 500, 500)

    GUICtrlCreateLabel($sPrompt, 10, 10, 480, 20)

    $hInput = GUICtrlCreateInput("", 10, 50, 480, 20)
    ; Limit the input to 10 characters
    GUICtrlSetLimit(-1, 10)

    $hButton_OK = GUICtrlCreateButton("OK", 10, 100, 80, 30)
    $hButton_Can = GUICtrlCreateButton("Cancel", 110, 100, 80, 30)

    GUISetState()

    GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

    While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $hButton_Can
                Return SetError(1, 0, "")
            case $hButton_OK
                Return GUICtrlRead($hInput)
        EndSwitch

    WEnd

EndFunc


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) = $hInput Then

        ; Read the text
        $sText = GUICtrlRead($hInput)
        ; If there are any banned characters
        If StringRegExp($sText, '&|||<|>|"') Then
            ; Remove them
            GuiCtrlSetData($hInput, StringRegExpReplace($sText, '&|||<|>|"', ""))
        EndIf

    EndIf

EndFunc   ;==>_WM_COMMAND

Please ask if you have any questions. :)

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

Here is an adaptation of a recent function I wrote which you might be able to use.

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#Include <GuiButton.au3>

_Input()

Func _Input()
    Local $iError = 0
    Local $sTitle = "Input Test"
    Local $hGUI = GUICreate($sTitle, 225, 55, Default, Default)
    Local $hInput = GUICtrlCreateInput("", 5, 5, 215, 20)
    GUICtrlSetLimit($hInput, 128)
    Local $hCancel = GUICtrlCreateButton("Cancel", 35, 30, 70, 20)
    Local $hOkay = GUICtrlCreateButton("OK", 120, 30, 70, 20)
    GUISetState(@SW_SHOW)

    $dll = DllOpen("user32.dll")
    While 1
        $msg2 = GUIGetMsg()
        If $msg2 = $hCancel Or $msg2 = $GUI_EVENT_CLOSE Then ExitLoop

        $sUserInput = GUICtrlRead($hInput)
        If StringRegExp($sUserInput, '[|?&"<>]') Then
            If  $iError = 0 Then
                GUICtrlSetBkColor($hInput, 0xFFA090)
                $iError = 1
            EndIf
        ElseIf $iError = 1 Then
            GUICtrlSetBkColor($hInput, 0xFFFFFF)
            $iError = 0
        EndIf

        If $msg2 = $hOkay Or (ControlGetFocus ($sTitle) = "Edit1" And _IsPressed("0D")) Then
            If $iError = 0 Then
                MsgBox(0, "Success", "The text is valid")
                ExitLoop
            Else
                MsgBox(262160, "Error", "The text you entered contains illegal characters.")
                _GUICtrlButton_SetFocus($hOkay)
            EndIf
        EndIf
    WEnd
    DllClose($dll)
EndFunc
Edited by czardas
Link to comment
Share on other sites

StringRegExp($sText, '&|||<|>|"') Then

That's the point :)

If have another question, but if is too long forget it

If i want to set for example only some character ( es. only ABC is allowed ) i can use the same StringRegExp with all the rest of character or there is another method? Thanks for your help

edit: Thanks czardas, is the same of Melba, you have post it at the same time ;)

Edited by johnmcloud
Link to comment
Share on other sites

The easiest way would be to use the hex value of the ascii characters. Look for this in the help file under StringRegExp()

To see how this works, replace line 24 from my example

If StringRegExp($sUserInput, '[|?&"<>]') Then

With this line

If StringRegExp($sUserInput, '[x22x26x3cx3ex3fx7c]') Then
Link to comment
Share on other sites

  • Moderators

johnmcloud,

To give you a bit of a hint, this will only accept upper and lowercase ABC characters:

; If there are any banned characters
If StringRegExp($sText, "(?i)[^ABC]") Then
    ; Remove them
    GuiCtrlSetData($hInput, StringRegExpReplace($sText, "(?i)[^ABC]", ""))
EndIf

See if you can work it out yourself. Ask if you really get stuck - a minimum of 2 hours from now. :)

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

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

×
×
  • Create New...