Anona
Anona

Reputation: 253

Why isn't my regex for at least 1 uppercase working?

I'm trying to make a create user thing in asp, and ofcourse i want to use regex so i'm sure its a good password they are entering. I've tried many things but still, every single FKIN time its says I still need to enter at least 1 uppercase eventhough my pass pretty much consist of Uppercase...

The regex for the Uppercase is the third one.

CODE :

        <asp:Panel runat="server" DefaultButton="btnCreateUser">  
            <table>
                <tr>
                    <td>Gebruikersnaam</td>
                    <td><asp:TextBox runat="server" ID="txtUsername" />
                    <asp:RequiredFieldValidator ControlToValidate="txtUsername" ForeColor="Red" ID="rfUsername" runat="server" ErrorMessage="U heeft geen gebruikersnaam ingevoerd.<br />"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>Wachtwoord</td>
                    <td>
                        <asp:TextBox runat="server" ID="txtPassword" />  
                        <asp:RequiredFieldValidator ControlToValidate="txtPassword" ForeColor="Red" ID="rfPassword" runat="server" ErrorMessage="<br />U heeft geen wachtwoord ingevoerd.<br />"></asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator runat=server display=dynamic
                            ControlToValidate="txtPassword" foreColor="Red"
                            ErrorMessage="<br />Het wachtwoord moet minstens 1 cijfer bevatten. <br />"
                            ValidationExpression="(?=.*\d)$" />
                        <asp:RegularExpressionValidator runat=server display=dynamic
                              ControlToValidate="txtPassword" foreColor="Red"
                              ErrorMessage="Het wachtwoord moet minimaal uit 6 tekenen bestaan. <br />" 
                              ValidationExpression="(.{8,})$" />
                        <asp:RegularExpressionValidator runat=server display=dynamic
                              ControlToValidate="txtPassword" foreColor="Red"
                              ErrorMessage="Het wachtwoord moet minimaal 1 hoofdletter bevatten.<br />" 
                              ValidationExpression="^(?=.*[A-Z])$" />
                    </td>
                </tr>
                <tr>
                    <td>Naam</td>
                    <td>
                        <asp:TextBox runat="server" ID="txtDisplayName" />  
                        <asp:RequiredFieldValidator ControlToValidate="txtDisplayName" ForeColor="Red" ID="rfDisplay" runat="server" ErrorMessage="U heeft geen display naam ingevoerd.<br />"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label Text="" ID="lblNewUserStatus" runat="server" />
                    </td>
                    <td>
                        <asp:Button Text="Aanmaken" runat="server" ID="btnCreateUser" OnClick="btnCreateUser_Click" />
                    </td>
                </tr>
            </table>
         </asp:Panel>

Upvotes: 2

Views: 61

Answers (3)

vks
vks

Reputation: 67968

^(?=.*[A-Z])$" 

This should be ^(?=.*[A-Z]).*$" .

                          ^^

Your original regex expects an empty string ^$,and the lookahead wants a capital letter so in effect it won't match anything.

You can combine all your regexes in one:

^(?=.*\d)(?=.*[A-Z]).{8,}$

Upvotes: 2

Alan Moore
Alan Moore

Reputation: 75222

If you want to require that there's at least one uppercase letter, [A-Z] is all the regex you need. Those multiple-lookahead password regexes you see everywhere are for doing the whole validation in one shot. Performing separate regex matches as you are, it's dead simple:

  • [0-9] matches a string that has at least one digit
  • [A-Z] matches a string with at least one uppercase ASCII letter, and
  • .{8} matches a string with at least eight characters.

Upvotes: 0

karthik manchala
karthik manchala

Reputation: 13640

You have to add .* in your validation pattern:

^(?=.*[A-Z]).*$
            ^^

Upvotes: 1

Related Questions