Current I use the following RegEx for the user to enter a password
^\w{8,16}$
Now as I understand, \w only allows a-z, A-Z, 0-9 and _ character. What I want to do is to allow ANY character but the length to be between 8 and 16. How do I get about doing it? Thanx a lot in advance :)
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .
$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.
There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
Firstly, use a word count for what you need rather than regex.
If you really must, then .{8,16}
should work, the .
matches a single char, no matter what it is.
EDIT: To preempt your next question which will surely be, what is a good password validation regular expression, you might want to check out some of these blogs:
http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/
http://www.zorched.net/2009/05/08/password-strength-validation-with-regular-expressions/
OR just look up 'password validation stackoverflow' on google
Try this:
^.{8,16}$
The dot matches a single character, without caring what that character is. The only exception are newline characters. By default, the dot will not match a newline character.
For the details, please visit: http://www.regular-expressions.info/dot.html.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With