0

Right now I need to duplicate a password expression validator for a website. The password is only required to be 8-25 characters (only alphabet characters) long. I thought this was weird and had been using this regex

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,25})

but it has to be optional to have a capital letter, special characters and/or numbers throughout the password. I'm not particularly apt at building regex's where there are optional characters. Any help would be be appreciated.

I am using asp.net's RegularExpressionValidator.

7
  • It is this way right now, what is your exact problem ? Commented Sep 13, 2013 at 15:23
  • If you enter just abcdefghijklmnop it fails and it should pass Commented Sep 13, 2013 at 15:24
  • Is there any other problem that you have ? Commented Sep 13, 2013 at 15:26
  • As an advisory, 8 alpha characters is a very weak password, even when using a random sequence rather than the words that most humans will try to use. howsecureismypassword.net - don't put your actual passwords in, just examples of the ones you expect to see. Commented Sep 13, 2013 at 15:28
  • @Tragedian I appreciate your comment, but I am just doing what I have been told. Even though I have expressed how weak it is. Commented Sep 13, 2013 at 15:29

2 Answers 2

3

This pattern should work:

^[a-zA-Z]{8,25}$

It matches a string consisting of 8 to 25 Latin letters.

If you want to allow numbers as well, this pattern should work:

^[a-zA-Z0-9]{8,25}$

It matches a string consisting of 8 to 25 Latin letters or decimal digits.

If you want to allow special characters as well, this pattern should work:

^[a-zA-Z0-9$@!]{8,25}$

It matches a string consisting of 8 to 25 Latin letters, decimal digits, or symbols, $, @ or ! (of course you can add to this set fairly easily).

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

3 Comments

How would I modify this for special chacaters $, @, !, ect
@Brett Your question didn't make any mention of special characters. What are you trying to do exactly?
@Jerry sorry, you are correct. I have modified the question. I just want somthing like Brett$234Stack to work
2

Your current regex won't work because it will accept special characters as from 9th character (and anything after the 9th character in fact, even a 26th character because you don't have the end of string anchor) .

You probably want something like this:

^(?=.*[a-z])[A-Za-z0-9]{8,25}$

This first makes sure there are lowercase alphabets (you mentioned that uppercase and digits are optional, so this makes obligatory lowercase) and then allows only uppercase and digits.

EDIT: To allow any special characters, you can use this:

^(?=.*[a-z]).{8,25}$

My understanding of your problem is that the password's first requirement is that it has to contain lowercase alphabet characters. The option now is that it can also contain other characters. If this isn't right, let me know.

regex101 demo

2 Comments

I take care of 26+ by limiting the text box to a length of 25.
@Brett Ahh, okay, that works for the character limit, but the regex would have still allowed password@.

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.