views:

207

answers:

2

Why does this markup ...

<asp:TextBox ID="TextBox1" runat="server" CausesValidation="False"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExValidatorTextBox1" runat="server" 
                                ControlToValidate="TextBox1" 
                                Text="Invalid date format." 
                                ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d">
</asp:RegularExpressionValidator>

... throw a validation error when I enter some nonsense in the TextBox like "a" and hit only the Enter or Tab key? I' like to have the validation not before I click on a submit button.

(I'm working with VS2008 SP1, .NET 3.5 SP1, if that matters.)

+1  A: 

Set the EnableClientScript property to false. This will completely prevent validation on the client side. Then on the server side, be sure to check the Page.IsValid property before proceeding.

Yaakov Ellis
I've just tried it to disable client script in the validator. Now the validation does not fire when I hit TAB but still when I hit ENTER.
Slauma
Well, I think I understand: On ENTER a postback occurs and the validation happens on server side - in contrast to hitting the TAB key.
Slauma
+1  A: 

To answer the question (Why does Enter or Tab key cause a validation on a TextBox?), when you press Enter, the DefaultButton will cause the form to post back. Pressing enter is akin to clicking on the submit button.

If you want to disable the postback on enter, there are several solutions suggested here, also see here and here for a JQuery based solution.

RichardOD
But there is absolutely no button on the page (not yet). The page is a simple test page containing only the TextBox and the Validator.
Slauma
Yes, but the form will submit on enter.
RichardOD
@Slauma- I've edited my answer- have a look at some of the suggestions in the link I sent out.
RichardOD
Thanks Richard, I wasn't aware that simply hitting the ENTER key causes a postback of a form.
Slauma