5

Shouldn't this regex validator make sure the user enters something in the textbox? For some reason an empty textbox passes through.

<asp:RegularExpressionValidator ID="revNumericValidator" runat="server" ValidationExpression="^\d+$"
                    ControlToValidate="tb1" ErrorMessage="Please enter blah (must be a number)." />

2 Answers 2

8

Validators by convention don't validate empty text. If you want to require a value, you have to also add a RequiredFieldValidator.

MSDN says

Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control.

Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure why this is the case, but with the ASP.NET validator controls I have always had to combine a RegEx validator with a Required field validator, as the RegEx one only seems to fire if there is text included in the field.

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.