Sign in to follow this  
Followers 0
MPlb

Validating fields

13 posts in this topic

Hi

This is my first attempt at creating an AutoIt piece of code. I've not programmed for almost 10 years and was used to a very procedural language. Anyhow, I digress...

What I want to do is get someone to type in a password twice and then make sure that the same password has been entered both times.

My code (see below) prevents the user from pressing the 'Submit' button until they have put something in both the name field and the two password fields.

I would like to get a warning if the passwords are different when the 'Submit' button is pressed but I'm not sure how to go about this.

Can someone point me in the right direction please?

CODE
#include

AutoItSetOption("TrayIconHide", 1) ; Hide the autoit tray icon

WinMinimizeAll()

Opt("WinTitleMatchMode", 2)

CreateNewRemoteDB()

Exit

Func CreateNewRemoteDB()

GUICreate("Create Remote Database",400,450)

GUICtrlCreateLabel ("Please complete the following form (all fields are mandatory):",55, 20)

GUICtrlCreateLabel ("Full Name:",20,65)

$FNAMEID=GUICtrlCreateInput ("",125,60,250)

GUICtrlCreateLabel ("Password:",20,145)

$PWD1ID=GUICtrlCreateInput ("",125,140,250,"",$ES_PASSWORD)

GUICtrlCreateLabel ("Re-enter Password:",20,185)

$PWD2ID=GUICtrlCreateInput ("",125,180,250,"",$ES_PASSWORD)

$Button_1 = GUICtrlCreateButton ("Submit", 90, 400, 100)

$Button_2 = GUICtrlCreateButton ("Quit", 220, 400, 100)

GUICtrlSetState( $Button_1, $GUI_DISABLE )

GUISetState ()

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $Button_1

$FULLNAME = GUICtrlRead($FNAMEID)

$PWD1 = GUICtrlRead($PWD1ID)

$PWD2 = GUICtrlRead($PWD2ID)

MsgBox(0,"Full name", $FULLNAME)

MsgBox(0,"Password 1", $PWD1)

MsgBox(0,"Password 2", $PWD2)

ExitLoop

Case $msg = $Button_2

Exit

Case Else

If StringLen(GUICtrlRead( $FNAMEID )) > 0 And StringLen(GUICtrlRead( $PWD1ID )) > 0 And StringLen(GUICtrlRead( $PWD2ID )) > 0 And GUICtrlGetState( $Button_1 ) = 144 Then GUICtrlSetState( $Button_1, $GUI_ENABLE )

If StringLen(GUICtrlRead( $FNAMEID )) < 1 and GUICtrlGetState( $Button_1 ) = 80 Then GUICtrlSetState( $Button_1, $GUI_DISABLE )

If StringLen(GUICtrlRead( $PWD1ID )) < 1 and GUICtrlGetState( $Button_1 ) = 80 Then GUICtrlSetState( $Button_1, $GUI_DISABLE )

If StringLen(GUICtrlRead( $PWD2ID )) < 1 and GUICtrlGetState( $Button_1 ) = 80 Then GUICtrlSetState( $Button_1, $GUI_DISABLE )

EndSelect

Wend

EndFunc

I'd like to use a function to do this validation but (amongst other things) I'm not sure how if the passwords are different that I can show the warning, clear the two fields and get them to type them in again.

Any help and/or advice would be gratefully received.

Thanks

Matthew

Share this post


Link to post
Share on other sites



Use the == operator.

;o)

(or


nothing is foolproof to the sufficiently talented fool..

Share this post


Link to post
Share on other sites

You could use stringcompare

CODE
$compare = StringCompare($PWD1,$PWD2,1)

Kerros===============================================================How to learn scripting: Figure out enough to be dangerous, then ask for assistance.

Share this post


Link to post
Share on other sites

Thanks Kerros

I've put the following line in the section where the button is pressed:

CODE
If StringCompare($PWD1,$PWD2,1) <> 0 Then MsgBox (0,"Warning","Passwords are not the same")

This correctly does the compare but what I'd then like to do is jump back to the point where I can enter the passwords again. As it is now the code just runs through after displaying the error as I'd expect it to because I've not told it to do anything else.

An alternative would be if I could do the 'stringcompare' after entering the second password but I have no idea how to do this.

My programming skills are so rusty...

Share this post


Link to post
Share on other sites

Try:

Do
    $pwd1 = InputBox("Enter 1st pwd","Enter 1st pwd","","*")
    $pwd2 = InputBox("Enter 2st pwd - needs to be the same as first pwd!","Enter 2st pwd - needs to be the same as first pwd!","","*")
Until StringCompare($pwd1,$pwd2,1) = 0; 3rd agrument 1 = case sensitive! default is case INsensitive and for pdws you dont want that!

MsgBox(0,0,"passwords match!")

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Thanks Sadbunny, I quite like that solution. It would be perfect if I could just get rid of the 'Cancel' button on the Inputbox.

Ideally I'd really like to validate the password fields on my GUI but I just can't see how I can throw the control back into them should they not match.

Now I know why I gave up programming 10 years ago. It's so frustrating... <_<

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks Sadbunny, I quite like that solution. It would be perfect if I could just get rid of the 'Cancel' button on the Inputbox.

Ideally I'd really like to validate the password fields on my GUI but I just can't see how I can throw the control back into them should they not match.

Now I know why I gave up programming 10 years ago. It's so frustrating... :)

Hmm... The cancel button is default in the inputbox... You could catch @error (if it's 1 then cancel has been pressed and the loop should also continue), or you could make your own GUI. Is the following more what you were thinking of? You get to enter two passwords, and only when both fields have something entered and when both entries match, the OK button becomes available...

#include <GUIConstants.au3>

$gui = GUICreate("pwd test",300,100)
$label1 = GUICtrlCreateLabel("Pwd 1:",10,10,40,20)
$label2 = GUICtrlCreateLabel("Pwd 2:",10,40,40,20)
$pwd1 = GUICtrlCreateInput("",50,10,280,20,$ES_PASSWORD)
$pwd2 = GUICtrlCreateInput("",50,40,280,20,$ES_PASSWORD)
$okbutton = GUICtrlCreateButton("OK",10,70,50,20)

GUICtrlSetState($okbutton,$GUI_DISABLE); initially disable OK button
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $okbutton Then _okClicked()
    If StringLen(GUICtrlRead($pwd1)) > 0 And StringCompare(GUICtrlRead($pwd1),GUICtrlRead($pwd2),1) = 0 Then
; now we know at least something was entered and that pwd's match - enable OK button
        GUICtrlSetState($okbutton,$GUI_ENABLE)
    Else
; either no password was entered or passwords do not match - disable OK button
        GUICtrlSetState($okbutton,$GUI_DISABLE)
    EndIf
    Sleep(100); go easy on cpu
WEnd

Func _okClicked()
    MsgBox(0,0,"OK clicked - passwords match")
    Exit
EndFunc

/edit: I seem to have posted the source code twice... Fixed now <_<

/edit2: corrected StringCompare function to be case-sensitive, tnx Corz

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

except perhaps with the case sensitivity flag: ", 1"..

StringCompare(GUICtrlRead($pwd1),GUICtrlRead($pwd2), 1)

;o)

(or


nothing is foolproof to the sufficiently talented fool..

Share this post


Link to post
Share on other sites

except perhaps with the case sensitivity flag: ", 1"..

StringCompare(GUICtrlRead($pwd1),GUICtrlRead($pwd2), 1)

;o)

(or

Corrected <_<

Tnx


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

This is the method I like:

; Won't let you out of loop until you enter a valid password and verify it:
While 1
    $sPW = InputBox("Password", "Input password: ", "", "*")
    If @error = 0 And StringLen($sPW) >= 8 Then
        If $sPW == InputBox("Password", "Verify password: ", "", "*") Then
            ExitLoop
        Else
            MsgBox(16, "Error!", "Password entries did not match." & @CRLF & "Try again...", 10)
        EndIf
    Else
        MsgBox(16, "Error!", "Invalid password - must be at least 8 characters." & @CRLF & "Try again...", 10)
    EndIf
WEnd

MsgBox(64, "Results", "$sPW = " & $sPW)

<_<


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

This is the method I like:

; Won't let you out of loop until you enter a valid password and verify it:
While 1
    $sPW = InputBox("Password", "Input password: ", "", "*")
    If @error = 0 And StringLen($sPW) >= 8 Then
        If $sPW == InputBox("Password", "Verify password: ", "", "*") Then
            ExitLoop
        Else
            MsgBox(16, "Error!", "Password entries did not match." & @CRLF & "Try again...", 10)
        EndIf
    Else
        MsgBox(16, "Error!", "Invalid password - must be at least 8 characters." & @CRLF & "Try again...", 10)
    EndIf
WEnd

MsgBox(64, "Results", "$sPW = " & $sPW)

<_<

That works too and is a lot less code, I thought of something like this too, only it doesn't return control to any GUI - maybe there's other options on the same GUI that need to be able to activated while the pwd is being entered... :) Also when you press Cancel it says "invalid password", but that is fixed easily :P


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

That works too and is a lot less code, I thought of something like this too, only it doesn't return control to any GUI - maybe there's other options on the same GUI that need to be able to activated while the pwd is being entered... <_< Also when you press Cancel it says "invalid password", but that is fixed easily :)

I think of it as a really bad practice to keep passwords in GUI controls anyway, and would avoid it this way:

#include <GUIConstants.au3>

$gui = GUICreate("pwd test", 300, 100)
$okbutton = GUICtrlCreateButton("OK", 10, 70, 50, 20)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $okbutton
            _okClicked()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _okClicked()
    While 1
        $sPW = InputBox("Password", "Input password: ", "", "*")
        If @error = 0 Then
            If StringLen($sPW) >= 8 Then
                If $sPW == InputBox("Password", "Verify password: ", "", "*") Then
                    ExitLoop
                Else
                    MsgBox(16, "Error!", "Password entries did not match." & @CRLF & "Try again...", 10)
                EndIf
            Else
                MsgBox(16, "Error!", "Password must be at least 8 characters long." & @CRLF & "Try again...", 10)
            EndIf
        Else
            WinActivate($gui)
            Return
        EndIf
    WEnd
    
    MsgBox(64, "Ta Da!", "Got matching password!")
    Exit
EndFunc   ;==>_okClicked

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Also works... I think it's a question of what's your goal, and taste <_<


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
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
Sign in to follow this  
Followers 0