2

Can someone help me to validate the following rules using a RegEx pattern

Max length : 15
Minimum length : 6
Minimum character count : 1
Minimum numbers count : 1
Consequent repeated character count : 2

2
  • 1
    This is probably possible, but will produce very complicated and hard to understand regex (you will have 2 problems: one problem with validating, second with complicated regex). Why not simple function with those checks? Commented Sep 13, 2010 at 8:06
  • I have already implemented a validation mechanism. But I'm not much comfortable with the RegEx pattern. Commented Sep 13, 2010 at 8:11

2 Answers 2

6
^                   # start of string
(?=.{6,15}$)        # assert length
(?=.*[A-Za-z])      # assert letter
(?=.*[0-9])         # assert digit
(?:(.)(?!\1\1))*    # assert no more than 2 consecutive characters
$                   # end of string

will do this. But this won't look nice (or easily maintainable) in JavaScript:

if (/^(?=.{6,15}$)(?=.*[A-Za-z])(?=.*[0-9])(?:(.)(?!\1\1))*$/.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I have looked for.. Thanks a lot.
2

I suggest you use a few different regex patterns to check for all those rules cause it will either be impossible or very complicated.

  • .length to check the first 2 rules
  • [a-z] (with case insensitive option) for the 3rd rule
  • \d for the 4th rule
  • (.)\1{2,} for the 5th rule, if this one matches the string contains 3+ character repetitions

2 Comments

The last one won't catch a password like a0!!!!. Also, \a-z is invalid, you probably mean [a-z].
@Tim, corrected my answer even tho yours has already been accepted

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.