CompareValidator is not working correctly
I would prefer to use the asp.net Validation control as I currently have other validation controls within the same view. I need the error messages to appear in the validation summary.
I have two textboxes and I need to make sure textboxA is a LessThan textbox.
I used CompareValidator and set the following properties:
- ControlToCompare: textboxB
- ControlToValidate: textboxA
- Operator: GreaterThan / Also tried LessThan
- Type: Date
Here's the problem:
- When I provide the time in the textbox and go to the textbox. an error is displayed. I thought if the expression would fix this, but it doesnt.
In the Click event for the Update button, I added the following code as I only need it to check if both textboxA / textboxB! = Null.
if(String.IsNullOrEmpty(textboxB.Text))
{
Debug.Write("Valid");
timeCompareValidator.IsValid = true;
}
Thanks in advance for your help.
Clare
I think you need to use CustomValidator and that will still allow you to use your ValidationSummary.
See here:
How do I use CompareValidator for times without dates?
If you understand correctly, you need to compare two dates.
I found this code in 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
You want to try changing the if statement:
if (!string.IsNullOrEmpty(textboxA.Text) && !string.IsNullOrEmpty(textboxB.text))
If you want to compare two dates or times server side use this solution
DateTime dt1 = Convert.ToDateTime(TextBoxA.Text);
DateTime dt2 = Convert.ToDateTime(TextBoxB.Text);
int result = dt1.CompareTo(dt2)
I would use CustomValidator ( http://msdn.microsoft.com/en-us/library/9eee01cx(VS.71).aspx ). BTW: Why are you calling the validator directly?
timeCompareValidator.Validate();
Typically validators are evaluated during Page.Validate () which is triggered by each button (unless CausesValidation is set to false)