-1

Regex left for (Must not contain sequences of letters or numbers) and (Do not repeat a number or letter more than 5 times).

I know its repeated question but I was not able to find combination of my requirement. When I tried to combine I was getting errors

Other than that I was able to do it I was trying for not repeating more than 5 but this one is not working

`^(?=.*?[a-zA-Z])(?=.*?[0-9])((.)\2{0,4}(?!\2)).{6,15}$`

Partially working one is ^(?=.*?[a-zA-Z])(?=.*?[0-9]).{6,15}$ I need to both conditions in it.

Developer
  • 279
  • 6
  • 22

1 Answers1

0

As suggested, have a RegExp that checks the string for allowed characters first. That should be simple and easy to read.

Then if that passes, split the string into an array of each character, loop over it, and check counts for each character. Something like Lodash would help with the latter, but you could write it in plain javascript too.

if (/^[a-zA-Z][0-9]$/.test(password)) {
    const chars = password.split('');
    const counts = {};
    chars.forEach(char => {
        if (counts[char] && counts[char] > 5) {
            isValid = false;
        } else {
            counts[char] = counts[char] ? counts[char] + 1 : 1;
        }
    });
    return isValid;
}
Steve Vaughan
  • 2,163
  • 13
  • 18