Jump to content

Input Validation Function


ViciousXUSMC
 Share

Recommended Posts

So a few times I have wanted/needed some sort of input validation (aka input mask) we had an old udf somebody made that sort of worked but was hard to use and a bit limited.  As I started to learn regex of late sort of by chance I realized it could be used as a pretty great input validation.  But it was a lot of code if you wanted to do it for multiple fields.  So wrapped it up into a little function and sharing.  Far from a UDF so putting in the example section, and I am not sure if what I am doing is even "good code" but it works for me and hope it is usefull for somebody.

Basic RegEx knowledge will help you make the most of this, but a few examples are included below in the code snippet. 

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

$Form1 = GUICreate("Test GUI", 618, 276, 192, 124)
$Label1 = GUICtrlCreateLabel("Test GUI", 168, 24, 47, 17)
$Button1 = GUICtrlCreateButton("Toggle Validation", 290, 24, 110, 40)
$Input1 = GUICtrlCreateInput("Max 10 Characters", 40, 72, 553, 21)
$Input2 = GUICtrlCreateInput("Numb3rs 0nly", 40, 97, 553, 21)
$Edit1 = GUICtrlCreateEdit("Max 10 Chars Numbers Only + Auto Line Break", 40, 120, 553, 105)
GUISetState(@SW_SHOW)

$iToggle = 1

While 1
 Sleep(10)
 If Mod($iToggle, 2) = 0 Then
;Example 1 Limit to 10 Characters
 _GUIRegExValidate($Input1, "(.{10})(.)", "$1")
;Example 2 Can only type numbers
 _GUIRegExValidate($Input2, "[^\d]", "")
;Example 3 Combind More Than One On Same Input limit to 10 characters, limit to numbers only, line break @ 10 characters to new line
 _GUIRegExValidate($Edit1, "(.{10})(.)", "$1" & @CRLF)
 _GUIRegExValidate($Edit1, "[^\d\r\n]", "")
 EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $iToggle = $iToggle + 1
    EndSwitch
WEnd

Func _GUIRegExValidate($sInputLabel, $sRegExCapture, $sRegExReplace)
    $Step1Read = GUICtrlRead($sInputLabel)
    $Step2Validate = StringRegExpReplace($Step1Read, $sRegExCapture, $sRegExReplace)
    If @Extended = 0 Then Return
    GUICtrlSetData($sInputLabel, $Step2Validate)
EndFunc

Or per recommendation you can use it with the GuiRegisterMsg so that you can only process the RegEx when you make a chance instead of in a constant loop. 

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

$Form1 = GUICreate("Test GUI", 618, 276, 192, 124)
$Label1 = GUICtrlCreateLabel("Test GUI", 168, 24, 47, 17)
$Button1 = GUICtrlCreateButton("Toggle Validation", 290, 24, 110, 40)
$Input1 = GUICtrlCreateInput("Max 10 Characters", 40, 72, 553, 21)
$Input2 = GUICtrlCreateInput("Numb3rs 0nly", 40, 97, 553, 21)
$Edit1 = GUICtrlCreateEdit("Max 10 Chars Numbers Only + Auto Line Break", 40, 120, 553, 105)
GUISetState(@SW_SHOW)

$iToggle = 1

GUIRegisterMsg($WM_COMMAND, '_WM_COMMAND')

While 1
    Sleep(10)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $iToggle = $iToggle + 1
    EndSwitch
WEnd

Func _GUIRegExValidate($sInputLabel, $sRegExCapture, $sRegExReplace)
    $Step1Read = GUICtrlRead($sInputLabel)
    $Step2Validate = StringRegExpReplace($Step1Read, $sRegExCapture, $sRegExReplace)
    If @Extended = 0 Then Return
    GUICtrlSetData($sInputLabel, $Step2Validate)
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
If Mod($iToggle, 2) = 0 Then
;Example 1 Limit to 10 Characters
 _GUIRegExValidate($Input1, "(.{10})(.)", "$1")
;Example 2 Can only type numbers
 _GUIRegExValidate($Input2, "[^\d]", "")
;Example 3 Combind More Than One On Same Input limit to 10 characters, limit to numbers only, line break @ 10 characters to new line
 _GUIRegExValidate($Edit1, "(.{10})(.)", "$1" & @CRLF)
 _GUIRegExValidate($Edit1, "[^\d\r\n]", "")
 EndIf
EndFunc

 

Edited by ViciousXUSMC
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...