views:

427

answers:

5

Hello all,

I would prefer to use an asp.net Validation Control as I currently have other Validation Controls within the same View. I need to error messages to be displayed wihtin a Validation Summary.

I have two textboxes and I need to make sure than textboxA is LessThan textboxB.

I have used the CompareValidator and have set the properties to:

  • ControlToCompare: textboxB
  • ControlToValidate: textboxA
  • Operator: GreaterThan / Also tried LessThan
  • Type: Date

Here is the problem:

  • When I provide a time within textboxA and move onto textboxB the validation error is displayed. I thought my if statement would fix this but it doesn’t.

Within the Click Event for the 'Update' button I have added the following code, as I only need it to validate if both textboxA/textboxB != null.

        if(String.IsNullOrEmpty(textboxB.Text))
        {
            Debug.Write("Valid");
            timeCompareValidator.IsValid = true;    
        }

Thanks in advance for any help.

Clare

+1  A: 

If I understand you correctly, you need to compare two dates.

I found this code on msdn:

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
// The example displays the following output:
//    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM
Steven
I am comparing to times, however the closest type I found was 'Date'. Can this be to converted to use the CompareValidator?
ClareBear
I'm not familiar with CompareValidator, sorry.Could it be that it dose not recognize your input as a date?
Steven
A: 

Do you want to try changing your if statement to:

if (!string.IsNullOrEmpty(textboxA.Text) && !string.IsNullOrEmpty(textboxB.text))
Ruffles
The same problem still happens.
ClareBear
A: 

If you want to compare two dates or times on the server side use this solution

DateTime dt1 = Convert.ToDateTime(TextBoxA.Text);
DateTime dt2 = Convert.ToDateTime(TextBoxB.Text);

int result = dt1.CompareTo(dt2)
msi
Would this be located within the - protected void checkTimes_TextChanged(object sender, EventArgs e)?
ClareBear
Yes, but it's be better if you locate this on the ButtonClick
msi
Where would the GreaterThan operator be located?
ClareBear
+1  A: 

I think you need to use a CustomValidator and that will still allow you to use your ValidationSummary.

See here:

http://stackoverflow.com/questions/497294/how-can-i-use-comparevalidator-for-times-without-dates

sihol
This is set to Date
ClareBear
A: 

I would use a CustomValidator (http://msdn.microsoft.com/en-us/library/9eee01cx%28VS.71%29.aspx). BTW: Why are you calling the validator directly?

timeCompareValidator.Validate();

Normally the Validators are evaluated during Page.Validate() which every Button fires (if CausesValidation is not set to false)

Arthur
I worked through a example and this told me to call it directly.
ClareBear