This discussion is archived
7 Replies Latest reply: Aug 8, 2013 6:36 AM by sabre150 RSS

Regex Password validator

AnjanN Newbie
Currently Being Moderated

I faced some problem while validating a password that will contain minimum 5 characters using regex.

I am using a very simple regex as below:

String REGEX_PATTERN = "(?=.*[a-z|A-Z]{5,}).{8,}";

It says password must contains minimum 5 characters(a-z|A-Z), and password length will be minimum 8 chars.

It is working fine for the string "aasaT124". But fails for the string "aa12sa4T".

Clearly the difference is regex matches only if the characters are sequential.

The String for which validation fails also contains minimum 5 characters but characters are not sequential.

Pls tell me where is the problem in my regex that introduces this problem. I just need to validate simply whether my string contains minimum 5 characters or not independent of any position or sequence.

  • 1. Re: Regex Password validator
    sabre150 Expert
    Currently Being Moderated

    The {5,} in the lookahead needs to refer to the the group (.*[a-zA-Z]) and not just the single character. Also you might want to make the '*' non-greedy.

     

    Are you wanting to include the '|' character in the mandated set? Presumably you meant it as meaning 'or' but in that context it is taken literally. Just remove it and use [a-zA-Z] .

     

    You say "contain minimum 5 characters using regex" yet your regex insists on a minimum of 8 characters not 5.

     

    The ',' in  {5,} is not needed since  lookahead is not constrained to matching the whole. It doesn't hurt and makes the intention more clear so probably best to leave as is.

  • 2. Re: Regex Password validator
    836548 Journeyer
    Currently Being Moderated

    The regex pattern should be like below :

     

    "(?=.*[a-z|0-9|A-Z]{5,}+).{8,}"

     

    it will work.

  • 3. Re: Regex Password validator
    masijade Explorer
    Currently Being Moderated

    836548 wrote:

     

    The regex pattern should be like below :

     

    "(?=.*[a-z|0-9|A-Z]{5,}+).{8,}"

     

    it will work.

    That does not fulfill the requirements, however.

  • 4. Re: Regex Password validator
    836548 Journeyer
    Currently Being Moderated

    masijade wrote:

     

    836548 wrote:

     

    The regex pattern should be like below :

     

    "(?=.*[a-z|0-9|A-Z]{5,}+).{8,}"

     

    it will work.

    That does not fulfill the requirements, however.

    Yes you are right..

     

    I think this cannot be achieved using regex (not sure) . You can use the javascript function or java method to validate the password.

  • 5. Re: Regex Password validator
    sabre150 Expert
    Currently Being Moderated

    836548 wrote:

     

    masijade wrote:

     

    836548 wrote:

     

    The regex pattern should be like below :

     

    "(?=.*[a-z|0-9|A-Z]{5,}+).{8,}"

     

    it will work.

    That does not fulfill the requirements, however.

    Yes you are right..

     

    I think this cannot be achieved using regex (not sure) . You can use the javascript function or java method to validate the password.

     

    As I explained in my first response it most definitely can be achieved using a regex. The change to the OP's original regex is almost trivial!

     

    P.S. Why do people think they need to use '|' inside a character class to mean 'or' ? 

  • 6. Re: Regex Password validator
    836548 Journeyer
    Currently Being Moderated

    sabre150 wrote:

     

    As I explained in my first response it most definitely can be achieved using a regex. The change to the OP's original regex is almost trivial!



      Dear sabre, i don't think it is possible, if you are sure, could you please give the regex for the same?

  • 7. Re: Regex Password validator
    sabre150 Expert
    Currently Being Moderated

    I am certain. I have explained what changes need to be made. I have to leave something for the OP to do!


    I'm not convinced the OP wrote the original regex. It was close to what he said was needed and since it used lookahead I have to assume that the writer of the regex was more than just a regex novice. If so then the writer should have been able to fix the problem even without my hints. I suspect the OP just copied the regex from some web site and was hoping that someone here would not realise that and give him the actual regex needed. I don't do that.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points