Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RangeValidator and dates 1

Status
Not open for further replies.

markharr

Vendor
May 21, 2001
87
AU
I'm not sure whether this is strictly a javascript question or a question peculiar to this control. Anyway, feel free to re-direct me if you think this is appropriate. I'm still learning ASP.Net so please be nice :)

Anyway, here is the problem: My ASP (this is .Net v1.1 BTW) page has two TextBox controls that contain date values (in short form). Bear in mind that in Australia, the date format in common use is the British format, DD/MM/YYYY.

I have two separate RangeValidator controls associated with these two TextBox controls. The minimum value is set at design time in Visual Studio 2003 and the maximum value of these RangeValidator controls is set in the Page_Load event to today's date.

The two controls are to display a hire date and a birth date. The birth date is displayed as 1/09/1958 (i.e. 1st of September 1958) and the hire date is displayed as 5/03/1994 (i.e. 5th of March 1994). The minimum value for both RangeValidation controls is set as 01/01/1900. The maximum range is then set (today) as 14/11/2005.

When I hit my Update button which causes validation, the hire date value is said to be out of range. This behaviour is in WebUIValidation.js. For the purposes of illustration, I have copied a brief extract of the relevant functions from the javascript file below.

RangeValidatorEvaluateIsValid checks that it is within the range by calling ValidatorCompare:


function RangeValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
return (ValidatorCompare(value, val.minimumvalue, "GreaterThanEqual", val) &&
ValidatorCompare(value, val.maximumvalue, "LessThanEqual", val));

}


The problem is when ValidatorCompare is asked to evaluate the comparison if hire is less than or equal to today's date (i.e. 14/11/2005 in this example).


function ValidatorCompare(operand1, operand2, operator, val) {
var dataType = val.type;
var op1, op2;
if ((op1 = ValidatorConvert(operand1, dataType, val)) == null)
return false;
if (operator == "DataTypeCheck")
return true;
if ((op2 = ValidatorConvert(operand2, dataType, val)) == null)
return true;
switch (operator) {
case "NotEqual":
return (op1 != op2);
case "GreaterThan":
return (op1 > op2);
case "GreaterThanEqual":
return (op1 >= op2);
case "LessThan":
return (op1 < op2);
case "LessThanEqual":
return (op1 <= op2); [highlight]This returns false[/highlight]
default:
return (op1 == op2);
}
}


Curiously, if I put a leading zero back into the date string (i.e. 05/03/1994) it validates correctly but I don't understand why the birth date validated correctly.

I don't know what I'm doing wrong but I would appreciate any guidance.
 
As a simple workaround, why don't you format the dates that are in the textboxes?

For example, you could have a calender control that the user picks the date from rather than a textbox (or as well as if you really want and just insert the selected date from the calendar into the textbox) then it will be formatted correctly.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I'm considering writing my own validator. The more I think about it, I think that that the range validator is fundamentally flawed when dealing with dates. It is really doing a string compare.

I might write a DateRangeValidator but this isn't trivial (if nothing else, to test) when you consider the number of permutations of date formats that are extant.

You're suggestion of using a calendar is another good idea. At the moment, I am just building a "pretend" system to learn ASP.Net and C# and I'm trying to keep it simple but it would be useful in a real system.
 
As I said at the outset, I am still learning this stuff so the solution is maybe something everybody else knows but here it is, just in case somebody else strikes this.

RangeValidators have a type property. The default is String but there are other possibilities and Date is one of them.

I found this not by looking in the documentation (though the information is there, I just didn't see it) but by debugging into the function ValidatorConvert.

Hopefully this will be helpful for somebody else in the future.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top