Reputation: 327
I have written a RegEx for password which takes any character with min length of 5 and maximum length of 30.
I've tried the following expression:
(\S){5,30}
but it also accepts passwords with more than 30 characters. How can I make sure it doesn't match such passwords?
Upvotes: 0
Views: 268
Reputation: 336478
Your problem is that your regex also matches substrings of your input.
\S{5}
(or (\S){5}
) matches 12345
in the string 1234567890
.
So you need to anchor your regex:
^\S{5,30}$
validates a 5-30 character, non-whitespace string. The parentheses around \S
are useless and unnecessary.
At any rate, why would you impose a length restriction on a password? And why wouldn't you allow whitespace characters in it? See also this.
Upvotes: 6
Reputation: 811
If you really want "any character" use the period (.
) instead of \S
.
Upvotes: 0