tags:

views:

137

answers:

2

I do not want to allow blank passwords, but if I leave the textbox blank, the text property from the regular expression does not show up until I enter at least 1 character. How do I allow it not to have a blank password? I tried using the + sign instead of the * sign, but it still did not work.

<asp:RegularExpressionValidator 
                                ControlToValidate="txtPassword" 
                                Display="Dynamic"
                                runat="server" 
                                Text="Invalid" 
                                ValidationExpression="^(?=.*\d{1})(?=.*[a-zA-Z]{2}).{7,}$"
                                ValidationGroup="UserRegistrationValidation">
</asp:RegularExpressionValidator>
+1  A: 

That regex does not match an empty string, so i can only assume that asp.net isn't invoke the validator, try adding a required validator as well.

Paul Creasey
I did and that works, the thing that annoys me about that is that if I put both validators next to each other, the RegularExpressionValidator error message is pushed way to the right of the text box.
Xaisoft
@Xaisoft - If you set Display="Dynamic" on both validators and then use the ErrorMessage property to set the appropriate messages, this should allow the error messages to occupy the same space.
Steve Wortham
@Steve = Thanks, I knew that had something to do with it. I thought I put Dynamic on the Required one, but I didn't, so it works now. ErrorMessage is only for ValidationSummary, right? I am just using the Text property to display the message.
Xaisoft
@Xaisoft - That's fine. The ErrorMessage property will affect the ValidationSummary if you have one, but it'll also display the message next to the control if you don't supply a Text property... http://www.codenewsgroups.net/group/microsoft.public.dotnet.framework.aspnet.webcontrols/topic11103.aspxPersonally, I just use the ErrorMessage.
Steve Wortham
+3  A: 

A RegularExpressionValidator doesnt validate empty text by design. You need also a RequiredFieldValidator or have a look at the following link: http://stackoverflow.com/questions/1085464/regularexpressionvalidator-not-firing-on-white-space-entry

Tim Schmelter