isvalid not working for a text box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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????
 
I think you meant sender not the eventargs parameter ??? might be wrong.

But no it doesnt like that anyway.

It says in my ASP.NET book you can check to see if individual controls are
valid by

[validatorcontrolid].IsValid

Why does it bypass the if statement ?!
 
The easiest way to debug javascript is to litter it with alerts...

put an alert just into your function to check it's firing, then an alert
(vdEndDate) to check it's a valid object, then alert (vdEndDate.IsValid),
etc... this'll help you narrow down which parameter is causing the problems.

Javascript just stops executing if there's an error, which can be annoying,
so another option is try/catch while also using alerts to check where your
functions running too...

Good luck.

Dan.

louise raisbeck said:
I think you meant sender not the eventargs parameter ??? might be wrong.

But no it doesnt like that anyway.

It says in my ASP.NET book you can check to see if individual controls are
valid by

[validatorcontrolid].IsValid

Why does it bypass the if statement ?!

Dan Bass said:
Should that not be

if ( e.IsValid )
{
//....
}

instead of vdEndDate?
 
i'm not writing javascript its C# on the server side checking if the web
control is valid!



Dan Bass said:
The easiest way to debug javascript is to litter it with alerts...

put an alert just into your function to check it's firing, then an alert
(vdEndDate) to check it's a valid object, then alert (vdEndDate.IsValid),
etc... this'll help you narrow down which parameter is causing the problems.

Javascript just stops executing if there's an error, which can be annoying,
so another option is try/catch while also using alerts to check where your
functions running too...

Good luck.

Dan.

louise raisbeck said:
I think you meant sender not the eventargs parameter ??? might be wrong.

But no it doesnt like that anyway.

It says in my ASP.NET book you can check to see if individual controls are
valid by

[validatorcontrolid].IsValid

Why does it bypass the if statement ?!

Dan Bass said:
Should that not be

if ( e.IsValid )
{
//....
}

instead of vdEndDate?

message 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????
 
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: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
i see. Very helpful to give me the order of exec, thanks so much.

On the same vein, I have been also getting a javascript error
Page..Validators.isvalid is null or not an object (something like that I'm at
home now) I wondered if this solution will solve that? I have had to disable
client scripts just to avoid it, obviously not ideal. Ring any bells ??
 
Back
Top