-->

ASP.NET Regular Expression Validator Not Working o

2019-07-21 11:51发布

问题:

I am using the following Regular Expression Validator to ensure the password enter meets the requirements of 1 upper case, 1 lower case, 1 number, and 1 allowed special character:

    <asp:RegularExpressionValidator ID="rev_txtNewPassword" runat="server" Display="Dynamic"
    ErrorMessage=" Password does not meet requirements" ControlToValidate="txtNewPassword" ValidationGroup="password"
    ValidationExpression="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$%^&*]).{8,16}$"></asp:RegularExpressionValidator>

This works in FireFox, Chrome, Safari, Internet Explorer 8 and 9. It just doesn't work on Internet Explorer 7. No matter what is entered into the password box, it says it doesn't meet the requirements. This is part of a custom SharePoint 2010 web part.

Any ideas why the expression validator isn't working only on IE7?

Thanks

回答1:

As the OP requests, I put the solution here so everybody can view this question as solved. The problem is caused by the IE regex lookahead bug and the original solution is made by Alan Moore. The solution can be found here:

Solution



回答2:

Even if it worked, how is the user supposed to know how to form a valid password that passes that expression? Are they supposed to keep guessing until they get it? If you need such a complex password, it might be polite to generate it for your users. Even if you list all the rules for the user, they will struggle with it once it gets too complex.

ASP.NET Membership has a basic password generator, e.g.

Membership.GeneratePassword(8, 2);

If you want something more sophisticated, you can roll your own using RNGCryptoServiceProvider.GetBytes() to generate an array of random bytes, then convert those bytes into characters, using a modulo to restrict the range.

Note also that you can set a password strength regex in Membership configuration:

<membership defaultProvider="SqlProvider"
  userIsOnlineTimeWindow = "20>
  <providers>
    <add
      name="SqlProvider"
      type="System.Web.Security.SqlMembershipProvider"
      connectionStringName="SqlServices"
      requiresQuestionAndAnswer="true"
      passwordStrengthRegularExpression="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$%^&*]).{8,16}$"
      />
   </providers>
</membership>