0

I've read a lot of Stack Overflow answers about preventing repeated characters, but these are all when the regex string is in one line, like this:

^(?=.{4})(?!.{32})(?!.*(.)\1{4})\w[\w.-]+\w$

I'm using this concept for a password validation http://www.webdesignerdepot.com/2012/01/password-strength-verification-with-jquery/ which means I want a valid class added to a list item each time one of the validation rules is passed. There is a list of instructions above the password field, like this:

<ul>
  <li id="letter" class="invalid">At least <strong>3 letters</strong></li>
  <li id="number" class="invalid">At least <strong>2 numbers</strong></li>
</ul>

<input type="password" id="input-password" name="password" autocomplete="off">

If the users input satisfies this, then the instruction gets a tick next to it.

So, I have:

var pwd = $(this).val();

if ( pwd.match(/[A-z]{3,}/) ) {
        $('#letter').removeClass('invalid').addClass('valid');
    } else {
        $('#letter').removeClass('valid').addClass('invalid');
    }
if ( pwd.match(/\d{2,}/) ) {
        $('#number').removeClass('invalid').addClass('valid');
    } else {
        $('#number').removeClass('valid').addClass('invalid');
    }

I was hoping the {3,} would work, but in this instance, it only validates if the 3 letters are consecutive, not if they occur anywhere in the complete string. e.g. "a1aa" is invalid but "aaa" is valid. I have read around a lot, but I'm just not understand what is needed in this situation, as I can't find other examples where the regex has been split up like this.

Any help?

Edit

Sorry, as nhahtdh pointed out, the title doesn't make sense in relation to the question! I actually have an additional validation requirement which is that the password must not repeat the same letter more than 3 times. For example, 'mississippi' would not be valid as it has more than 3 instances of the letter 's'.

5
  • What do you mean by "at least 3 letters" - does it mean that the input contains any 3 letters at any position? Another confusing thing is that - how does preventing repeated character is relevant to your question?
    – nhahtdh
    May 1, 2013 at 7:53
  • Sorry nhahtdh, my mistake, got myself confused with what I was asking. I've added some more comments under the 'edit'. In answer to your first question, the password must have at least 3 letters (a-z in upper or lowercase) in any position. May 1, 2013 at 12:43
  • So at least 3 letters (don't care repeated), and at least 3 different letters are 2 of the requirements?
    – nhahtdh
    May 1, 2013 at 13:03
  • 1
    Let's hope I don't have to login to this system you're creating :) May 1, 2013 at 18:16
  • We've had that argument and lost :( - My unenviable role is to make these intense security requirements seem simple to the user. I put the idea in js fiddle if you're interested jsfiddle.net/davidpauljunior/agatg May 1, 2013 at 22:48

2 Answers 2

2

To match a string with at least three letters A-Z a-z you could use

pwd.match( /(?:[^a-z]*[a-z]){3,}/i ) 

The i is the case-insensitive modifier.

The regex will match zero or more characters that are not A-Za-z, followed by a character A-Za-z, three or more times.

The ?: is optional and makes the group () non-capturing.

Incidentally, it is more efficient to use test rather than match if you're not actually using the match, e.g.

if ( /([^a-z]*[a-z]){3,}/i.test(pwd) ) {

Further to comment

To meet your additional validation requirement you could use

/^(?:[^a-z]*([a-z])(?!(?:.*?\1){3})){3,}[^a-z]*$/i

It will only match a string with at least three letters, and it uses a negative look-ahead (?! and back-reference \1 to ensure the same letter (regardless of case) does not appear more than three times.

Or, if e.g. "abcccC" is okay because the fourth C is uppercase then instead you could use

/^(?:[^A-Za-z]*([A-Za-z])(?!(?:.*?\1){3})){3,}[^A-Za-z]*$/
4
  • Since you are so awesome at creating these, I would bet that he might want the 'same letter instance count' requirement to be separate from the 'min a-z' requirement. May 1, 2013 at 18:20
  • Yes! It's working, thanks MikeM. Chris your bet would come in, as I do want the 'same letter instance count' to be separate from the 'min a-z' requirement. May 1, 2013 at 22:42
  • @MikeM are you able to help me split the same letter instance count from the min a-z? I have had a go but I must be missing something. May 2, 2013 at 22:23
  • @davidpauljunior. For the 'no more than 3 of same letter' check you could use if /([A-Za-z])(.*?\1){3}/.test(pwd) { // invalid.
    – MikeM
    May 3, 2013 at 10:32
1

How about

if ( pwd.match(/[a-zA-Z]/g).length >= 3 ) {
5
  • I'd rather write >= 3, since it is closer to the description - make it easier to detect bugs.
    – nhahtdh
    May 1, 2013 at 7:36
  • 2
    I was kinda thinkin the same thing when answering. No problem there. Updated May 1, 2013 at 7:47
  • 1
    Actually, A-z will include garbage between Z and a. Make it A-Za-z, or A-Z with i flag.
    – nhahtdh
    May 1, 2013 at 7:51
  • Downvoted until the A-z is corrected. This will also throw an exception if there is no match, as null has no length.
    – MikeM
    May 1, 2013 at 8:06
  • @MikeM Good call on the null issue, didn't think of that. Did consider the A-z problem, but was using the values the OP supplied. Your answer definitely works better. The workaround for the null check is ugly, so I'm not even going to post it ;) May 1, 2013 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.