简体   繁体   中英

Why regularexpressionvalidator is being ignored?

I have implemented a regex using regularexpressionvalidator but it doesn't work.

It's like that it's ignoring the validator. Why ?

  <div class="form-group">
                            <label class="control-label">
                                New Password
                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtnewPassword"
                                     ErrorMessage="*" Display="Dynamic" ForeColor="red" ValidationGroup="acc"></asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="regexValNewPassword" runat="server" Display="Dynamic" ForeColor="red" ValidationGroup="acc"
                                    ControlToValidate="txtnewPassword"
                                    EnableClientScript="False"
                                    ErrorMessage="***"
                                    ValidationExpression="^[1-9]+[0-9]*$" Visible="True">
                                    *
                                </asp:RegularExpressionValidator>

                            </label>
                            <asp:TextBox runat="server" ID="txtnewPassword" TextMode="Password" CssClass="form-control passwords"></asp:TextBox>

                        </div>

Update:

See.. I want my regex to throw an error when I only input a string of digits eg 123455. It should only accept mixed chars string eg 12ac3b12 or aac2211@11

You can use this regex that will not match if the input is only digits. It will only match if the input has at least one digit and one alphabet, as per your current requirements. Here minimum password length is 2 and you can make it any length by specifying number here {N,} You can further tweak the regex in case you have similar other requirements regarding character set or let me know and I can further update the regex.

^(?=.*[A-Za-z])(?=.*\d).{2,}$

Demo,

https://regex101.com/r/Fv7cw7/1

Your regex ^[1-9]+[0-9]*$ matches only digits and should start with 1-9.

To check if there is at least a character and a digit you could use a positive lookahead.

^(?=.*[A-Za-z@])(?=.*\\d)[@a-zA-Z\\d]+$

Regex demo

Explanation

  • ^ Start of the string
  • (?=.*[A-Za-z@]) Positive lookahead to assert 1 character from the character class
  • (?=.*\\d) Positive lookahead to assert 1 digit
  • [@a-zA-Z\\d]+ Match 1+ times what is allowed in the character class. You can add to the character class what you would allow to match.
  • $ End of the string

Due to the 2 positive lookaheads, the minimum length is 2 characters. If you want to match a minimum length like for example 8, you can add a quantifier like {8} to the character class instead of a + .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM