views:

1491

answers:

4

When I enter 9:00 into the Start control, and 16:00 into Finish; the code below fails validation.

Does anybody know a way I can use the ASP.NET validator for times without the dates?

Start <asp:TextBox ID="txtStart" runat="server" /> (hh:mm)
<br />
Finish <asp:TextBox ID="txtFinish" runat="server" /> (hh:mm)
<br />
<asp:CompareValidator
    id="cpvFinish"
    ControlToValidate="txtFinish"
    ControlToCompare="txtStart"
    Operator="GreaterThanEqual"
    Type="Date"
    Display="Static"
    EnableClientScript="true"
    ErrorMessage="Finish time must be later than the start time."
    runat="server" />

PS-I know I can easily use CustomValidator instead, it just seems like something this validator should be able to handle.

A: 

try to change the Type property to String, it should work.

Marwan Aouida
That was the first thing I tried. But since '9' in '9:00' is greater than the '1' in '16:00', it fails validation.
John MacIntyre
you can use 09:00 in palce of 9:00,that should work fine
Marwan Aouida
Thanks, but I don't want to depend on the user to enter the zero. I don't want to add any more client side js either. Thanks anyway.
John MacIntyre
A: 

Compare validator allowable types are: Stinrg, Integer, Double, Date (no time) Currency

Tom
+1  A: 

This appearently cannot be done.

For the record; I used a custom validator.

EDIT: Here's my custom validator code in case anyone (i.e. alhambraeidos) needs it. Please note, this code replaces my sample control names (txtStart & txtFinish) with actual names from my app (txtReady & txtClose).

try
{
    // parse & compare dates
    string randomDt = "01/01/2000 ";
    args.IsValid = DateTime.Parse(randomDt + txtClose.Text) > DateTime.Parse(randomDt + txtReady.Text);
}
catch(Exception /*ex*/)
{
    // exception is assumed to be invalid times entered
    args.IsValid = false;
}
John MacIntyre
A: 

How is the custom validator for it, please ???

Alhambra Eidos
I'm assuming you are asking how I did it, so I updated my answer with my exact code. http://stackoverflow.com/questions/497294/how-can-i-use-comparevalidator-for-times-without-dates/538635#538635
John MacIntyre