Jump to content

Recommended Posts

Posted

I'm having trouble in making an input box where I can limit the characters that can be inputted.  Anybody have an idea how this can be done in Auto IT?

Specifically I would like to create an input field where the user would enter a MAC address.  What I would like to do is to limit the characters that can be inputted to only HEX values (A-F and 0-9 as well as the - character).  This is so that the user cannot input invalid characters. The program is going to be used by very non-technical people, so the idea is to remove as many chances for errors that we can think of.  I would also prefer to limit the field to only 17 characters, so that there is only enough space to enter the MAC address with the - character as the separator.

Thank you all in advance for any suggestions!

Posted (edited)

I have been trying to learn more about RegEx recently, so I'm no expert, but I think this will work.

Do
   Local $sValue = InputBox("Testing", "Enter the MAC Address.", "", " M17")
Until StringRegExp($sValue, '([0-9a-fA-F]{2}[-]){5}([0-9a-fA-F]{2})') = 1

 

Edited by MuffinMan
Slight edit to allow for lowercase a-f
Posted

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

This should work to do it dynamically  :)

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

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 232, 90)
$Label = GUICtrlCreateLabel("Enter MAC address", 22, 8, 163, 17)
$input = GUICtrlCreateInput("", 22, 30, 165, 24)
GUICtrlSetLimit(-1, 17)
GUICtrlSetFont(-1, 12)
GUISetState()

GUIRegisterMsg($WM_COMMAND, '_WM_COMMAND')
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

While 1
  Sleep(10)
WEnd

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 Local $id = BitAND($wParam, 0x0000FFFF)
 Local $code = BitShift($wParam, 16)
   If $id = $input AND $code = $EN_UPDATE Then
       Local $content = GUICtrlRead($input)
       Local $len = StringLen($content)
       Local $char = StringRight($content, 1)
       Local $mod = Mod($len, 3)
       If not (( ($mod = 1 or $mod = 2) and StringRegExp($char, '[[:xdigit:]]') ) OR _ 
           ($mod = 0 and $char = "-")) Then GUICtrlSetData($input, StringTrimRight($content, 1)) 
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Func _Exit()
    Exit
EndFunc

 

Posted

Mikell,

Thank you very much!  That is EXACTLY what I was looking for!    mLipok and Muffinman, thank you as well for your suggestions!

Posted

Mikell,

I'm not sure what I am missing, but when I paste in your additional code, it errors out. (the original one works great!)

By the way, excuse my ignorance, but where in the code does it specifically state that only a-f and 0-9 and - are allowed?  I'm trying to figure this out, because your code is very useful, and I could see myself using it for other projects as well, where I would want to limit what characters can be inputted.  Seeing as how in other projects I might want to limit the input to a different set of characters, I would like to know how to change it.

 

 

Posted (edited)

When you create an input box you can set the length of characters, digits only, Uppercase, Center like this.

GUICtrlCreateInput Style Options

$ES_NUMBER        ==  Accepts only digits [0-9]
$ES_UPPERCASE   ==  Converts all characters to uppercase
$ES_LOWERCASE  ==  Converts all characters to lowercase
$ES_CENTER          ==  Centers text

GUICtrlSetLimit ( controlID, max , OPTIONAL min )  == so you can set a max and min character length for an input control
 

 $UserInput = GUICtrlCreateInput("", 120, 292, 161, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER,$ES_NUMBER))
  GUICtrlSetLimit($UserInput, 10) ; Limit User Input to 10 Characters, can also just use -1 instead of variable of input box if this line is directly under like here

 

 

Edited by iAmNewbe
Posted

iAmNewbe,

Thanks! that worked great!  I see that the additional code that mikell sent me was to make the MAC address always appear as uppercase, no matter how it was typed, but thanks to the style options you showed me for GUICtrlCreateInputStyle, I simply added the "$ES_UPPERCASE" style command and it now displays the MAC address always in upper case, no matter how it is typed.

 

Posted

There are more options but the rest are mainly display of the box itself those I put above are the most useful. You can also use RegEx as MuffinMan shows for more involved things but if all you want is to limit to digits only that is built in using that style option.  The manual is an important read, I think over the last two weeks I went through it like a 100 times.  

Posted (edited)
  On 10/26/2016 at 10:20 PM, GeorgeB said:

where in the code does it specifically state that only a-f and 0-9 and - are allowed?

Expand  

Sorry for the lack of explanations  :>

In fact the code actually forbids the last character written if not Hex or '-' depending on the length of the string
Here it is, detailed

Local $content = GUICtrlRead($input)
; get the string length
Local $len = StringLen($content)   
; get the last character entered
Local $char = StringRight($content, 1)  
; Mod returns 1 if length=1, 2 if length=2, 0 if length=3, 1 if length=4, etc
Local $mod = Mod($len, 3) 
 
; if NOT ( Mod = 1 or 2 AND the last char is hexadecimal, or Mod = 0 AND the last char is '-' )
If not (( ($mod = 1 or $mod = 2) and StringRegExp($char, '[[:xdigit:]]') ) OR _ 
           ($mod = 0 and $char = "-")) Then 
; then remove the last char and write to the input
       GUICtrlSetData($input, StringTrimRight($content, 1)) 

; i.e. if the last character written is in 5th position, then string length = 5, Mod = 2, so this char must be hexa. If it is not then it will not be written
; and so on

:)

Edited by mikell
Posted

I was working on this a while back, wanted to make it a UDF but never put the time to write it to standards.

I used it for quite a few things however.

#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

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 _GUIRegExValidate($Edit1, "(.{20})(.)", "$1")
EndFunc

 

Posted

Thanks Mikell for the clarification, and viciousxusmc for that code as well!

Thanks to everybody for all of the great info!  It has been extremely helpful!

 

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
×
×
  • Create New...