Jump to content

Form validations


Recommended Posts

I am starting out using AutoIt. Here is a simple form with username and password. I want to check if information entered is valid once user clicks a button.

My problem now is that it only validates once. E.g.: if I type 5 character username, it will complain it is not 7 character (good). But once I correct that mistake and press the button again it will still say the same thing.

Do I need to have a loop?


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

Opt("GUIOnEventMode", 1)

$main = GUICreate("Test Tool", 600, 600)
$hyourlabel = GUICtrlCreateLabel("YOUR CREDENTIALS", 30, 10, 256)
GUICtrlSetFont($hyourlabel, Default, 600)

Local $adminfrejalabel = GUICtrlCreateLabel("Username:", 8, 38, 64, 17)
Global $adminfrejaid = GUICtrlCreateInput("", 80, 38, 110, 17)

Local $adminpasswordlabel = GUICtrlCreateLabel("Password:", 8, 62, 64, 17)
Global $adminpassword = GUICtrlCreateInput("", 80, 62, 110, 17, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL))

$userButton_Check = GUICtrlCreateButton("VALIDATE", 32, 480, 85, 25)
GUICtrlSetOnEvent($userButton_Check, "startvalidation")


While 1

Func startvalidation()

   $adminfrejaid    = GUICtrlRead($adminfrejaid)
   $adminpassword   = GUICtrlRead($adminpassword)

   If StringLen($adminfrejaid) <> '7' Then
      MsgBox($MB_SYSTEMMODAL, "User ID", "Please enter exactly 7 characters.")

   If StringLen($adminpassword) < '5' Then
      MsgBox($MB_SYSTEMMODAL, "Your Password", "Please enter a valid password.")


Func ExitGui ()
Exit ; Exit the program


Link to comment
Share on other sites

Change your while loop to this:


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
        case $userButton_Check

Besides, there are parameters for Input that can also help you to validate things.

Edited by Xenobiologist

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

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

  • Create New...