Jump to content

Input Filter - Allow Only Date ##/##/####


Recommended Posts

I want to filter an input to Only allow dates to be entered. 2 digit month, 2 digit day and 4 digit year(##/##/####). This is what I have so far.

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

;I Modified some of Melba23's code

$Frm = GUICreate("Input Filter", 300, 30, -1, -1)
Global $inTest = GUICtrlCreateInput("", 5, 5, 290)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)


While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd

GUIRegisterMsg($WM_COMMAND, "")

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)   ;HiWord

    If $iIDFrom = $inTest And $iCode = $EN_CHANGE Then

    $Read_Input = GUICtrlRead($inTest)
    $Read_Input1 = ""
    If StringRegExp($Read_Input, '[^\d/]') Then $Read_Input = StringRegExpReplace($Read_Input, '[^\d/]', '\1')
    $Point1 = StringInStr($Read_Input, "/", 2, 2)
    $Point2 = StringInStr($Read_Input, "/", 2, 3)
    If $Point1 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point1 + 4)
    If $Point2 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point2 - 1)
    GUICtrlSetData($inTest, $Read_Input1 & $Read_Input)
    EndIf

EndFunc;==>_WM_COMMAND
[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!
Link to comment
Share on other sites

Maybe...

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

$Frm = GUICreate("Input Filter", 300, 30, -1, -1)
Global $date = GUICtrlCreateDate("", 5, 5, 290)

; to select a specific default format
    $DTM_SETFORMAT_ = 0x1032
    $style = "MM/dd/yyyy"
    GUICtrlSendMsg($date, $DTM_SETFORMAT_, 0, $style)

GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd


MsgBox(0, "Time", GUICtrlRead($date))

8)

NEWHeader1.png

Link to comment
Share on other sites

Maybe...

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

$Frm = GUICreate("Input Filter", 300, 30, -1, -1)
Global $date = GUICtrlCreateDate("", 5, 5, 290)

; to select a specific default format
    $DTM_SETFORMAT_ = 0x1032
    $style = "MM/dd/yyyy"
    GUICtrlSendMsg($date, $DTM_SETFORMAT_, 0, $style)

GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd


MsgBox(0, "Time", GUICtrlRead($date))

8)

I like that even better! More GUI the better! Thanks!
[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!
Link to comment
Share on other sites

  • Moderators

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

;I Modified some of Melba23's code

Global $Frm, $inTest
$Frm = GUICreate("Input Filter", 300, 30, -1, -1)
$inTest = GUICtrlCreateInput("", 5, 5, 290)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)


While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd

GUIRegisterMsg($WM_COMMAND, "")

Func MY_WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam

    Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
    Local $iCode = BitShift($wParam, 16)   ;HiWord
    Local $sRead = ""
    
    Switch $iIDFrom
        Case $inTest
            Switch $iCode
                Case $EN_CHANGE
                    $sRead = GUICtrlRead($inTest)
                    If StringRegExp($sRead, "^\d{2}/\d{2}/\d{4}\z") = 0 Then
                        If StringRegExp($sRead, "^\d{2}[^/]|^\d{2}/\d{2}[^/]|^\d{2}/\d{2}/\d{4}.") Then
                            $sRead = StringRegExpReplace($sRead, "(\D)", "")
                            $sRead = StringRegExpReplace($sRead, "^(\d{2})", "$1/")
                            $sRead = StringRegExpReplace($sRead, "^(\d{2}\D\d{2})", "$1/")
                            $sRead = StringRegExpReplace($sRead, "^(\d{2}/\d{2}/\d{4})(.*?\z)", "$1")
                            GUICtrlSetData($inTest, $sRead)
                        EndIf
                    EndIf
            EndSwitch
    EndSwitch

EndFunc;==>_WM_COMMAND

Edited by SmOke_N
Cut down one of the regex's

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Very well done my man. How long will it take you to make it work in different locales?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Maybe...

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

$Frm = GUICreate("Input Filter", 300, 30, -1, -1)
Global $date = GUICtrlCreateDate("", 5, 5, 290)

; to select a specific default format
    $DTM_SETFORMAT_ = 0x1032
    $style = "MM/dd/yyyy"
    GUICtrlSendMsg($date, $DTM_SETFORMAT_, 0, $style)

GUISetState(@SW_SHOW)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
WEnd


MsgBox(0, "Time", GUICtrlRead($date))

8)

How would I change the default start date?

GUICtrlSetData($date,"01/01/2009") doesn't work.

[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!
Link to comment
Share on other sites

Over-Kill Maybe... :D

lol

8)

Nahh. He has too much hair so I'm trying to get him to tear some of it out.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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