11

I'm making a password checking. One of those functions is to find if the inputted password is consecutively repeated. I do not have codes yet because I don't know how to do it.

I found this one RegEx match two or more same character non-consecutive but it only match repeated commas.

Here's are the scenarios:

5236aaa121 - Repeated pattern because a is consecutively repeated 3 times

2312aa32aa - No repeated character

111111asd - Repeated pattern because 1 is consecutively repeated many times

Community
  • 1
  • 1
catherine
  • 22,492
  • 12
  • 61
  • 85
  • 4
    If someone wants to repeat a character in their password multiple times, you should let them. – Justin Helgerson Mar 28 '13 at 17:21
  • yeah I know, this is just a message for them – catherine Mar 28 '13 at 17:22
  • 3
    You're actually reducing the security of your system by adding these kind of arbitrary restrictions. – user229044 Mar 28 '13 at 17:22
  • How many times can a character be repeated? Is it okay for it to be repeated twice? – Justin Helgerson Mar 28 '13 at 17:22
  • This is not a restriction Ok. – catherine Mar 28 '13 at 17:22
  • 1
    If you warn the user about it, it will become a restriction because the user will think "Oh, I've been warned about that; I shouldn't do it". – Chowlett Mar 28 '13 at 17:24
  • I know all of your opinions but my boss want this feature. Do I have to say no to his request??? – catherine Mar 28 '13 at 17:25
  • This is what my boss said: `I like the idea of letting the customer pick whatever password they want, but for those not familiar with password strength we should at least warn them as to how secure their password may or may not be. ` – catherine Mar 28 '13 at 17:25
  • 1
    many times you should say no to requests from management. – Derek Mar 28 '13 at 17:25
  • 1
    most places just show a 'strength meter', where you calculate how strong it is and just show them graphically. give them a link to a popup or hidden div with ideas for making their password more secure. – Derek Mar 28 '13 at 17:27
  • 5
    If you don't want to answer that's ok. I will just research it. – catherine Mar 28 '13 at 17:27
  • 5
    @Derek your not my boss to tell me on what I'm going to do. If you don't want to answer just ignore this question. I'm just following orders. – catherine Mar 28 '13 at 17:28
  • What platform are you on? I posted a solution below that will work on most, but back references can be implementation specific... – jmar777 Mar 28 '13 at 17:30

5 Answers5

19

Use a back reference: /(.)\1\1/

Example:

var hasTripple = /(.)\1\1/.test('xyzzzy');

JSFiddle Example

jmar777
  • 38,796
  • 11
  • 66
  • 64
  • how to implement this one? I don't know how to use it. – catherine Mar 28 '13 at 17:34
  • As an example, try this code: `var patt=/(.)\1\1/; alert(patt.test("1234567890o")); alert(patt.test("Moo")); alert(patt.test("Mooo"));` – Scott Mermelstein Mar 28 '13 at 17:36
  • What platform are you on? Is this in JavaScript, or server-side somewhere? – jmar777 Mar 28 '13 at 17:36
  • Sweet. Doesn't sound like you need it anymore, but here's a quick jsfiddle I threw together: http://jsfiddle.net/t87wB/ – jmar777 Mar 28 '13 at 17:40
  • Also (I wanted to keep this remark separate from answering your actual question), I definitely echo some of the above remarks about pushing back if this is supposed to help password security. Maybe have your boss ask a question about it on SO as well :) – jmar777 Mar 28 '13 at 17:42
  • yeah I will really ask him about this one and I will tell him about all of your opinions. I don't know what's his reason about this. I never ask question before, I just do what his request on the site – catherine Mar 28 '13 at 17:47
  • One more question, if I input `&&&`, is it return to True? – catherine Mar 28 '13 at 17:48
  • `&&&` will show up as 3 consecutive characters, so that will be considered invalid as well. – jmar777 Mar 28 '13 at 17:50
  • Thanks for the edit, @letiagoalves. I modified it a bit again to use a smaller example, but thanks :) – jmar777 Mar 28 '13 at 17:53
  • @jmar777 you did well. I prefer to use **match function** instead because it gives me more info but for this case **test function** will do it – letiagoalves Mar 28 '13 at 17:55
6

How about the following one?

(.)\1{2,}

Steve Seo
  • 61
  • 1
3

Try this regex: (.)\1\1+

/(.)\1\1+/g

The dot matches any character, then we are looking for more than one in a row. i tested it on http://regexpal.com/ and i believe it does what you want

you can use this like this:

str.match(/(.)\1\1+/g).length

just check that it is 0

to see this in action.... http://jsfiddle.net/yentc/2/

2

You just iterate the string by for loop and compare one to next if both are same then increase by one(declare one variable for count).. At last check count value if it is greater then 0 then the string is repeated pattern...

Kanagaraj M
  • 956
  • 8
  • 18
1

You could do something like this:

var password = '5236aaa121';

for(var i = 0; i< password.length; i++) {
    var numberOfRepeats = CheckForRepeat(i, password, password.charAt(i));
    //do something

}

function CheckForRepeat(startIndex, originalString, charToCheck) {
    var repeatCount = 1;
    for(var i = startIndex+1; i< password.length; i++) {
        if(originalString.charAt(i) == charToCheck) {
            repeatCount++;
        } else {
        return repeatCount;
        }   
    }
    return repeatCount;
}
jugg1es
  • 1,560
  • 18
  • 33