GeorgeB Posted October 26, 2016 Posted October 26, 2016 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!
MuffinMan Posted October 26, 2016 Posted October 26, 2016 (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 October 26, 2016 by MuffinMan Slight edit to allow for lowercase a-f
mLipok Posted October 26, 2016 Posted October 26, 2016 https://www.autoitscript.com/wiki/User_Defined_Functions and... 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 Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24
mikell Posted October 26, 2016 Posted October 26, 2016 This should work to do it dynamically expandcollapse popup#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 GeorgeB, triple_N, Skysnake and 1 other 2 2
GeorgeB Posted October 26, 2016 Author Posted October 26, 2016 Mikell, Thank you very much! That is EXACTLY what I was looking for! mLipok and Muffinman, thank you as well for your suggestions!
mikell Posted October 26, 2016 Posted October 26, 2016 A slight optional addition for a better look Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam) ;.... If not (( ($mod = 1 or $mod = 2) and StringRegExp($char, '[[:xdigit:]]') ) OR _ ($mod = 0 and $char = "-")) Then GUICtrlSetData($input, StringTrimRight($content, 1)) Else GUICtrlSetData($input, StringUpper($content)) EndIf GeorgeB 1
GeorgeB Posted October 26, 2016 Author Posted October 26, 2016 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.
iAmNewbe Posted October 26, 2016 Posted October 26, 2016 (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 October 26, 2016 by iAmNewbe triple_N and GeorgeB 1 1
GeorgeB Posted October 26, 2016 Author Posted October 26, 2016 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.
iAmNewbe Posted October 26, 2016 Posted October 26, 2016 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.
mikell Posted October 27, 2016 Posted October 27, 2016 (edited) 10 hours ago, GeorgeB said: where in the code does it specifically state that only a-f and 0-9 and - are allowed? 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 October 27, 2016 by mikell
ViciousXUSMC Posted October 27, 2016 Posted October 27, 2016 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. expandcollapse popup#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 Professor_Bernd and Skysnake 2
GeorgeB Posted October 27, 2016 Author Posted October 27, 2016 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!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now