1

I have a validator to check password length and password characters but it doesn't seem to work.

This is what I have:

    <asp:RegularExpressionValidator ID="RegExp1" runat="server"    
    ErrorMessage="Password length must be between 7 to 10 characters"
    ControlToValidate=" txtPassword "    
    ValidationExpression="^[a-zA-Z0-9'@&#.\s]{7,10}$" />

When I type in a password for example: P@55w0rd123#@! it still tells me the "Password length must be between 7 to 10 characters" and won't register the user then?

Not sure why this is not working?

UPDATE

I can put in a password of: 1234567890 and it works but when I try and put in a more complex password it gives the above error.

6
  • 1
    your expression restricts the number of characters from 7 to 10. What you gave as an exemple "P@55w0rd123#@!" has 14 characters. Actually, it looks it works as intended in the code and in the error message returned looks clear and consistent with it Commented Oct 25, 2019 at 7:55
  • 1
    it's not clear why you think "it's not working". What do you expect ? that passwords with 14 chareacters are allowed ? or not ? Commented Oct 25, 2019 at 7:57
  • 2
    Oh sorry I see! had a seriously stupid moment there!!! Thanks! Commented Oct 25, 2019 at 7:58
  • 2
    Why a max password length? You aren't storing it clear text in a varchar(10) field are you? Commented Oct 25, 2019 at 8:10
  • 1
    Why restrict the types of characters that you can enter either? You'd be better off just validating the min-length and set it to something like 10 or 12. Commented Oct 25, 2019 at 8:11

2 Answers 2

3

In the sample P@55w0rd123#@! you have 2 errors:

  1. Length must be 7 - 10.
  2. Password not include '!' symbol
Sign up to request clarification or add additional context in comments.

Comments

0

One of the problems you've got, is that your validator is trying to do too many things at once, which is leading to your confusion. You're making an assumption that your sample password is correct, which isn't being helped by the generic error that you're receiving. If your error was more specific, you'd see the problem more easily.

Whilst it is possible to validate the password length, the characters, etc all in one go, it doesn't result in a helpful message for the user.

Consider breaking your password requirements down into individual validators, so you can give the user more helpful feedback.

To start with, you could do something similar to this:

<asp:RegularExpressionValidator ID="RegExp1" runat="server"    
ErrorMessage="Password length must be between 7 to 10 characters"
ControlToValidate="txtPassword"    
ValidationExpression="^[.]{7,10}$" />

<asp:RegularExpressionValidator ID="RegExp2" runat="server"    
ErrorMessage="Password must only contain the allowed characters (letters, spaces and numbers both upper and lower case, and any of the following special characters '@&#.)"
ControlToValidate="txtPassword"    
ValidationExpression="^[a-zA-Z0-9'@&#.\s]*$" />

This way, when the password is the incorrect length, but all valid characters, you will only show the length error. When the password is the correct length, but contains disallowed characters, you will only see the character error. When the password length is incorrect, and also has invalid characters, you will correctly see both errors.

You could take this one step further, and tell the user which disallowed characters they entered, such as "the ! character is not allowed". This will greatly help your user to find a valid password more easily.

Password requirements are a great source of frustration to users, it's one of the areas of your signup process where you will lose the most people. Even small improvements to usability can have a significant difference in account creation conversion rates.

Comments

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.