Jump to content

Regex for password validation


Recommended Posts

Hallo members,

Looking for the right regex for password validation, the password must be eight characters long including at least 1 number and includes both lower and uppercase letters and at least 1 special character.

€ , £ and letters with umlauts, accents, etc. are not allowed.

I have made a test gui with two different validations, this is just for testing purposes, but is this right way to do it?

'((?=.*\d)(?=.*[A-Z])(?=.*\W).{8,8})'

and

'([A-Za-z: ]+\$([A-Z0-9]+)([\s]*[A-Za-z: ]+\$([A-Z0-9]+)){0,2})'

 

The test GUI

#NoTrayIcon
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.12.0
    Author:         myName

    Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

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

Opt("MustDeclareVars", 1)

Local $GUI, $Password, $Button1, $Button2
$GUI = GUICreate("Form1", 412, 261, 192, 124)
$Password = GUICtrlCreateInput("", 96, 32, 193, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_PASSWORD))
GUICtrlSendMsg(-1, $EM_SETCUEBANNER, False, "8 characters")
GUICtrlSetLimit(-1, 8, 8)
$Button1 = GUICtrlCreateButton("Export", 97, 77, 193, 25)
$Button2 = GUICtrlCreateButton("Import", 97, 123, 193, 25)
GUISetState(@SW_SHOW)

While 1
    Local $StringPassw = GUICtrlRead($Password)
    Local $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1 ; Export
            If $StringPassw = "" Then
                MsgBox(16, "Error!", "Password can not be empty")
                GUICtrlSetState($Password, $GUI_FOCUS)
                ;  regex for testing, the password must be eight characters including one uppercase letter, one special character and alphanumeric characters.
            ElseIf StringRegExp($StringPassw, '((?=.*\d)(?=.*[A-Z])(?=.*\W).{8,8})') Then
                MsgBox(0, "Strong", "Password is 8 characters including 1 uppercase letter, 1 special character, alphanumeric characters") ; Debug
                ; Export()
                GUICtrlSetState($Button1, $GUI_DISABLE)
            Else
                MsgBox(64, "Password is not strong!", "Re-type password, Password must be 8 characters including 1 uppercase letter, 1 special character, alphanumeric characters")
                GUICtrlSetData($Password, "")
            EndIf
        Case $Button2 ; Import
            If $StringPassw = "" Then
                MsgBox(16, "Error!", "Password can not be empty")
                ;  another regex for testing
                GUICtrlSetState($Password, $GUI_FOCUS)
            ElseIf StringRegExp($StringPassw, '([A-Za-z: ]+\$([A-Z0-9]+)([\s]*[A-Za-z: ]+\$([A-Z0-9]+)){0,2})') Then
                MsgBox(0, "Strong", "Password is 8 characters including 1 uppercase letter, 1 special character, alphanumeric characters") ; Debug
                ; Import()
                GUICtrlSetState($Button2, $GUI_DISABLE)
            Else
                MsgBox(64, "Password is not strong!", "Re-type password, Password must be 8 characters including 1 uppercase letter, 1 special character, alphanumeric characters")
                GUICtrlSetData($Password, "")
            EndIf
    EndSwitch
WEnd

 

searched the forum, but good not find a good example

Edit;

the password must be eight characters long including at least 1 number and includes both lower and uppercase letters and at least 1 special character.

€ , £ and letters with umlauts, accents, etc. are not allowed.

 

I need only a single regular expression

Thanks in advance

 

 

Edited by Mecano
Link to comment
Share on other sites

ViciousXUSMC, thanks for the answer, both topics I find already and they are very useful,  but I need a good pattern.

Sorry I forget to tell that , £ and letters with umlauts, accents, etc. are not allowed.

(?=.*\W) this will find any non-word character

Link to comment
Share on other sites

jguinch,

thanks, your code works,

but :>

let say aABbgh@q is now valid too

My first question is wrong, sorry
The good question must be:

at least 1 number and includes both lower and uppercase letters and at least 1 special character

 

 

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...