Sign in to follow this  
Followers 0

[SOLVED] Inputbox Rules


11 posts in this topic

Posted (edited)

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

Share this post


Link to post
Share on other sites



Posted

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

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

Thanks, i have a basic GUI for the inputbox and a OK button, i don't have posted because i think was easy to check if an inputbox contains a specific letter. But i'm wrong :)

Thanks for help

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

Thanks guys, all clear

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