Sign in to follow this  
Followers 0
JFee

Simple RegEx problem driving me insane!

7 posts in this topic

I am trying to find a way to only allow numbers to be entered into an Input box. I don't care how, I figured I'd use regex, but I can't get it to disallow non-numeric input.

Any ideas?


Regards,Josh

Share this post


Link to post
Share on other sites



#include <EditConstants.au3>
$Input = GUICtrlCreateInput("",$X,$Y,$W,$H,BitOr($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
That's great, thanks! I figured I was just missing something.

This is perfect for some of my inputs, but what about inputs that I want to contain decimals?


Regards,Josh

Share this post


Link to post
Share on other sites

That's great, thanks! I figured I was just missing something.

This is perfect for some of my inputs, but what about inputs that I want to contain decimals?

Here's one way

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

$Form1 = GUICreate("Float input", 251, 93, -1, -1)
$Input1 = GUICtrlCreateInput("", 8, 32, 233, 21)
$EnterReal = GUICtrlCreateLabel("Enter number", 8, 12, 67, 17)
GUISetState(@SW_SHOW)

Global $lastvalid = ''
GUIRegisterMsg($WM_COMMAND, "DB_WM_COMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func DB_WM_COMMAND($hWnd, $imsg, $iwParam, $ilParam)
    $nNotifyCode = BitShift($iwParam, 16)
    $nID = BitAND($iwParam, 0x0000FFFF)
    $hCtrl = $ilParam
    
    If $nNotifyCode = $EN_CHANGE Then
        If $ilParam = GUICtrlGetHandle($Input1) Then
            $sIP = GUICtrlRead($Input1)
        ;ConsoleWrite($sIP & @CRLF)
            Local $end = StringRight($sIP, 1), $valid = False
            Switch $end
                Case '0' To '9'
                    $valid = True
                Case '.'
                    If StringInStr($sIP,'.') = StringLen($sIP) Then $valid = True
            EndSwitch
            If Not $valid Then
                GUICtrlSetData($Input1, $lastvalid)
            Else
                $lastvalid = $sIP
            EndIf
        EndIf
        
    EndIf

    Return $GUI_RUNDEFMSG
    

EndFunc  ;==>DB_WM_COMMAND

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Hi,

this might interesting too

#include<GUIConstantsEx.au3>
#include <GUIConstants.au3>

$GUI = GUICreate("Enter a name for a new folder....", 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, 0x00000018); WS_EX_ACCEPTFILES
$file = GUICtrlCreateInput("", 10, 20, 300, 20)
$btn = GUICtrlCreateButton("Ok", 40, 95, 60, 20)
GUISetState(@SW_SHOW)

Dim $previousText

While 1
    $msg = GUIGetMsg()
    If $msg = $btn Or $msg = $GUI_EVENT_CLOSE Then Exit

    $text = GUICtrlRead($file)
    If $previousText <> $text Then ToolTip("")

    If StringRegExp($text, '\\|/|:|\*|\?|\"|\<|\>|\|') Then
        GUICtrlSetData($file, StringRegExpReplace($text, '\\|/|:|\*|\?|\"|\<|\>|\|', ""))
        DllCall("user32.dll", "int", "MessageBeep", "int", 0xFFFFFFFF)  ;Beep
        Local $tooltipPos = WinGetPos($GUI)
        ToolTip("A file name cannot contain any of the following characters:" & @LF & _
                '              \ / : * ? " < > |', $tooltipPos [0] + 160, $tooltipPos [1] + 60, Default, Default, 3)
        $previousText = GUICtrlRead($file)
    EndIf
WEnd

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Great! This post was kind of "pre-fetch-answering" :D to an idea i'll have to solve next days... :D

I modified your nice sample in a way to force an input and to prevent from entering "nothing":

#include-once
#include<GUIConstantsEx.au3>
#include <GUIConstants.au3>

MsgBox(0, "Ordnername", OrdnerName())

Func OrdnerName()
    Local $text
    $GUI = GUICreate("Bitte Namen für neuen Ordner eingeben", 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, 0x00000018); WS_EX_ACCEPTFILES
    $file = GUICtrlCreateInput("", 10, 20, 300, 20)
    $btn = GUICtrlCreateButton("Ok", 40, 95, 60, 20)
    GUISetState(@SW_SHOW)
    Local $tooltipPos = WinGetPos($GUI)
    $tooltipPos[0] += 60
    $tooltipPos[1] += 70
    Local $previousText
    While 1
        $msg = GUIGetMsg()
        If $msg = $btn Or $msg = $GUI_EVENT_CLOSE Then
            If StringLen($text) Then
                GUIDelete()
                Return $text
            Else
                ToolTip("Sie *MÜSSEN* einen Namen eingeben!", $tooltipPos[0], $tooltipPos[1], Default, Default, 4) 
                ; (....,3) didn't give me a visible tooltip -- why?
            EndIf
        EndIf
        $text = GUICtrlRead($file)
        If $previousText <> $text Then ToolTip("")
        If StringRegExp($text, '\\|/|:|\*|\?|\"|\<|\>|\|') Then
            GUICtrlSetData($file, StringRegExpReplace($text, '\\|/|:|\*|\?|\"|\<|\>|\|', ""))
            DllCall("user32.dll", "int", "MessageBeep", "int", 0xFFFFFFFF) ;Beep
            ToolTip("Ornernamen dürfen diese Zeichen nicht enthalten:" & @LF & _
                    '              \ / : * ? " < > |', $tooltipPos[0], $tooltipPos[1], Default, Default, 4)
            $previousText = GUICtrlRead($file)
        EndIf
    WEnd
EndFunc   ;==>OrdnerName

:) thanks :(

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

You are welcome!


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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