Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Password Regex

Last post 09-05-2008, 6:11 AM by prometheuzz. 9 replies.
Sort Posts: Previous Next
  •  09-05-2008, 2:50 AM 46007

    Password Regex

    Hi there

    I am very new to regex expressions in JAVA. I have the following requirements to validate a password:

    1) password length of minumum 10 characters

    2) at least 4 letters, anywhere in the password

    3) at least 4 numbers, anywhere in the password

    4) No special characters
     

    Any help would greatly be appreciated.

     

  •  09-05-2008, 2:57 AM 46008 in reply to 46007

    Re: Password Regex

  •  09-05-2008, 4:49 AM 46009 in reply to 46008

    Re: Password Regex

    Thank you for your response. 

    I tried the regex expression in that thread, but I am still not getting it right.

    I am validating this string: GH43S3UU74

    with this regex: ^(?=.{8,})(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[-#!$%_=+<>]).*$

     

    and it still fails. Also, where do I specify the minimum number of characters for the letters and the numbers?

  •  09-05-2008, 4:59 AM 46010 in reply to 46009

    Re: Password Regex

    Well, that's not surprising: I see you just copied and pasted the regex from that other thread. Are your requirements and the requirements in that other thread the same?

  •  09-05-2008, 5:06 AM 46011 in reply to 46010

    Re: Password Regex

    Yes I did copy and paste it, but the string that I am trying to validate should pass right?
  •  09-05-2008, 5:08 AM 46012 in reply to 46009

    Re: Password Regex

    Here's an explanation of that regex:

    ^                     the start of the string
    (?=.{8,})             when looking ahead, there must be at least 8 characters of any type
    (?=.*?\d)            
    when looking ahead, there must be at least 1 digit
    (?=.*?[a-z])          when looking ahead, there must be at least 1 lower case letter
    (?=.*?[A-Z])          when looking ahead, there must be at least 1 upper case letter
    (?=.*?[-#!$%_=+<>])   when looking ahead, there must be at least 1 of the following characters: -, #, !, $, %, _, =, +, < or >
    .*                    zero or more characters
    $                     the end of the string

  •  09-05-2008, 5:25 AM 46013 in reply to 46012

    Re: Password Regex

    Thanks for that explanation, it makes more sense now.

    So I changed my expression to look like this: ^(?=.{10,})(?=.{4,}?\\d)(?=.*{4,}[a-zA-Z]).*$

    But when I validate this string "GJK2VNDHS1", it passes when it should fail because there are only 2 numbers when 4 are required. Have I set up the occurrences in the pattern incorrectly?

    The same for this string "9692D63C49", it passes when it should fail because there are only 2 letters when 4 are required.

     

     

  •  09-05-2008, 5:45 AM 46014 in reply to 46013

    Re: Password Regex

    Another explanation (I left out one of the ? mark, since it's not really needed in this case):

    (?=A)          when looking ahead, there must be an 'A' straight away
    (?=.A)         when looking ahead, there can be any (single) character, followed by an 'A'
    (?=.*A)        when looking ahead, there can be any number of characters, followed by an 'A' (this is
                   the same as when saying: "
    when looking ahead, there must be at least one 'A'")
    (?=.*A.*A)     when looking ahead, there must be at least two 'A'
    (?=(.*A){2})   exactly the same as the previous

  •  09-05-2008, 6:05 AM 46015 in reply to 46014

    Re: Password Regex

    Thanks, it works perfectly now.
  •  09-05-2008, 6:11 AM 46016 in reply to 46015

    Re: Password Regex

    seedollar:
    Thanks, it works perfectly now.

    Good to hear it, you're welcome of course!

View as RSS news feed in XML