Sign in to follow this  
Followers 0
Mecano

Regex for password validation

7 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



This might help you:

 

And Just recently I needed something similar and the code in this thread was very easy to use and worked perfectly:

 

Share this post


Link to post
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

Share this post


Link to post
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

 

 

Share this post


Link to post
Share on other sites

jguinch, That's the pattern I need, thank you

Solved

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

  • Similar Content

    • rcmaehl
      By rcmaehl
      Hi all,
      I still suck at regex as always and I need some help. According to the regex tester I normally use this should be working fine but it doesn't....
      StringRegExp($sString, "\A[1-9]+[0-9]*(\-[1-9]+[0-9]*)?,*\Z") I basically want to match:
      all numbers EXCEPT 0, but including 10, 20, etc with each number separated by a comma and allowing a "-" separated range as a value For example:
      1-5,7,10-12 I've spent a couple hours modifying it but I'm not sure where I've gone wrong. Any help would be appreciated!
    • ISI360
      By ISI360
      Hi!

      I need a little bit help from some RegEx experts please:
      I would make my ISN AutoIt Studio faster when generating the scripttree. And what would be better to do this via regex?
      Problem is i am not really good at this regex stuff. So maybe someone could help me here.
       
      The challange is to get all Global Variables from a script via RegEx in a Array.
      Here is a example script with some tests:
      Global $Var1 = 1234 Local $Local_Var = 1234 $Ignore_me_too = 1234 Global $Var2 = 1234, $var3 = 1242 Global $ahIcons[30], $ahLabels[30] Global Const $Var4 = iniread($inivar1,"jj","jj","") , $var5= iniread($inivar2,"jj","jj","") Global $Var_String = "was" Global $Array_Test[16] = [1,15,16,0,31,15,25,15,25,30,8,30,8,15,1,15] Global Enum $MARGIN_SCRIPT_NUMBER = 0, $MARGIN_SCRIPT_ICON, $MARGIN_SCRIPT_FOLD Global Const $Delim = '\', $Delim1 = '|' Global $hard1 = "a", _ $hard2 = "b", _ $hard3 = "c"  
      The returning array should look like this:
      $Var1 $Var2 $var3 $Var4 $var5 $Var_String $Array_Test $MARGIN_SCRIPT_NUMBER $MARGIN_SCRIPT_ICON $MARGIN_SCRIPT_FOLD $Delim $Delim1 $hard1 $hard2 $hard3  
      I already made some success with a expression i found in the SciTE Jump Tool:  (\$\w+)(?:[\h\[.=+*/^,)\-])?
      This nearly returns the perfect results. But it does not check if it´s a global variable (with the const and enum options) and also returns variables in commands (for example $inivar1)
      I also found this regex: (?im:^(?=Global|Const|Enum|Static)(?:Global)?\h*(?:Const|Enum|Static)?(?:(?<=Enum)\h+Step\h+[+*-]\d+)?\h*)([^\r\n .\=]+)
      This returns also usefull results...but trying to understand this explodes my head

      Maybe someone can help me here?
      Thanks in advance!
    • VIP
      By VIP
      I can not do anything with any user in the "Local Users Group"
      I tried from my account but I could not change my password (ie set new password)!
      But I was able to activate the Administrator account and I logged into the Administrator account and still could not do anything with my account! But I can set the password and disable the Administrator account from my account or Administrator.
      Any ideas? (Ignore the click on the Sign-in option in the Change Accout settings.)
       

    • TheAutomator
      By TheAutomator
      Can anyone tell me why this isn't working?..
      #include <array.au3> $regexp = StringRegExp("test 'a b c'", "'([^']|'')*'|\S+", 3) _ArrayDisplay($regexp) trying to split this "test 'a b c'  'some other '' test'' ...'" into:
      0: test
      1: 'a b c'
      2: ...
      but it gives me:
      0: test
      1: c
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.