On the server side, there is an order of execution.
Page_Load
TextChanged
OnClick
PreRender
Validation automatically fires during the OnClick event, due to the Button
control calling Page.Validate() for you. Prior to this point, all validators
are at their default state of IsValid=true.
In your TextChange method, simply do this:
vdEndDate.Validate();
if (vdEndDate.IsValid) ....
--- Peter Blum
www.PeterBlum.com
Email:
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
"louise raisbeck" <> wrote in
message news:E1724CC6-A435-4BEB-A043-...
> Hi, I have a text box with corresponding comparevalidator as so..
>
> <tr><td align="right">End Date :</td>
> <td><asp:TextBox id="dtEndDate" runat="server"
> onTextChanged="set_Duration"
> autopostback="true" cssClass="mandatory"/></td>
> <td><asp:CompareValidator id="vdEndDate" runat="server"
> cssClass="ErrorMessage" ControlToValidate="dtEndDate" Type="Date"
> Operator="DataTypeCheck" ErrorMessage="Incorrect date format"/>
> </td></tr>
>
> As you can see when the user changes the text in dtEndDate, i want some
> code
> to fire. However I only want this code to fire if they have entered a
> correct
> date format, tested by vdEndDate, my validator control. So in my script to
> fire I have this:
>
> public void set_Duration(Object sender, EventArgs e) {
> if (vdEndDate.IsValid) {
> blah blah
> }
> }
>
> But when i put in a silly date and exit the field, the code still fires
> and
> errors coz my date format is wrong for the code i've written. Why is it
> not
> escaping the if condition?? I even see the validator control error message
> pop up briefly before it bombs. So surely vdEndDate is not valid when it
> enters my code????