2

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

ClareBear
  • 1,493
  • 6
  • 25
  • 47
  • Do you want to compare dates entered in textBoxA and TextBoxB on client side or on server side? – msi Sep 25 '09 at 09:10
  • I think I need both. I have added the properties to the client side. But I am seeing whether it is validated on the serverside. – ClareBear Sep 25 '09 at 09:20

4 Answers4

0

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
  • 19,224
  • 47
  • 152
  • 257
  • I am comparing to times, however the closest type I found was 'Date'. Can this be to converted to use the CompareValidator? – ClareBear Sep 25 '09 at 09:12
  • I'm not familiar with CompareValidator, sorry.Could it be that it dose not recognize your input as a date? – Steven Sep 25 '09 at 11:47
0

Do you want to try changing your if statement to:

if (!string.IsNullOrEmpty(textboxA.Text) && !string.IsNullOrEmpty(textboxB.text))
Ruffles
  • 79
  • 1
  • 3
0

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
  • 3,202
  • 4
  • 27
  • 37
0

I would use a CustomValidator (http://msdn.microsoft.com/en-us/library/9eee01cx(VS.71).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
  • 7,939
  • 3
  • 29
  • 46