1

I am trying to create a regular expression that validates a user's password when they create it. The password needs to be:

  1. at least 6 characters long and can be any type of character
  2. each character that is used needs to appear in the password exactly 3 times.

Good examples:

AAABBB
ABABBA
+++===
+ar++arra
myyymm
/Arrr/AA/

Does anyone know which regex would accomplish this?

11
  • 1
    I can give you an answer, but please explain first why you would want to have passwords with three times less entropy than necessary...? Commented Dec 15, 2016 at 7:48
  • 1
    Thinking about it a bit more, I'm not quite sure that this can be accomplished with just a regex; definitely not with a JavaScript regex with its very limited capabilities. You'd need two steps: Sort the password first, then use a regex to check that each character occurs exactly three times. Commented Dec 15, 2016 at 7:54
  • 1
    This is not a great match for regex (and a terrible password scheme). If you're targeting ES6 you can toss all the characters in a map and count them. Less efficiently, you can sort the string and then check it contains runs of 3 (which you could come up with a regex for but it would be more convoluted than almost any other alternative). Commented Dec 15, 2016 at 7:55
  • 2
    This looks like homework, no one would demand such stupid restrictions on a password Commented Dec 15, 2016 at 7:55
  • 1
    @sabithpocker - Almost all password strength checkers are obsolete and useless. Nowadays the conditions for a good password are only two: sufficient length (more is better) and resistance to dictionary attacks (which is the problem itself). Tips as "use capital an small letters, numbers and special symbols" have no value, even for average hackers. Commented Dec 15, 2016 at 8:22

3 Answers 3

2

You can ease yourself by sorting the password before testing it:

$('button').on('click', function(){
var s = $('#password').val();
var split = s.split("").sort().join("");
if(/^(?:(.)\1{2}(?!\1)){2,}$/.test(split))
   console.log("Valid password");
else
   console.log("Invalid password");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input id="password">
<button>Test me</button>

Sign up to request clarification or add additional context in comments.

4 Comments

this doesn't account for the overall length of 6 characters, correct?
minimum of 6 not exactly 6 :)
:) @ThomasAyoub
This will validate passwords like AAAAAA - it only checks for multiples of threes, not exactly three instances of each character.
0

For 1st option you have to check without regex.

var str = "your password";
var pattern = /(.)(.*\1){3}/;
if(str.length >= 6){
   // It will give you boolean as return
   // If it is match then it return true else false
   pattern.test(str);
}

1 Comment

You should try this with some of the test expressions provided.
0

An alternative solution since you're allowing code (which your question imply you wouldn't ;)

Using a function like verifyPass below should do the trick. It gradually replaces any valid three letter combination with an empty string. Checking that this is done in more than one iteration (it's at least 6 characters) and ending up with an empty string in the end, means it's a valid password.

function verifyPass(pass) {
  var re = /^(.)((?:(?!\1).)*)\1((?:(?!\1).)*)\1((?:(?!\1).)*)$/,
      cnt=0;

  while(re.test(pass)) {
    pass = pass.replace(re, '$2$3$4');
    cnt++;
  }
  return pass==='' && cnt>1;
}

var testItems = [
      '123123123',
      'AAABBB',
      'AAABBBAAA',
      'Qwerty',
      'ABABBA',
      '+++===',
      '111',
      'qweqwd',
      'sdcjhsdfkj',
      '+ar++arra',
      'mYYYmms',
      '/Arrr/AA/'
  ];
  
  testItems.forEach(function(item) {
    document.write('<span style="color:' + (verifyPass(item) ? 'green' : 'red') + ';">' + item + '</span> <br/>');
  });

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.