Listing 78 Format Datejs the Java Script used by the Reformat Date control

// JScript source code

// JScript source code function isLeapYear(year) {

var bIsLeapYear;





function FormatDate(ControlName) {

var ctrl; var text; var dt; var slash1; var slash2; var loop; var mo; var da; var yr;

var bIsDate;

var arrMonthLen=new Array(-1,31,28,31,30,31,30,31,31,30,31,30,31);


ctrl=window.event.srcElement; text=ctrl.value; dt=Date(text); slash1=text.lndexOf('/');


tstr=text.substring(0,2); mo=parseInt(tstr,10);

tstr=text.substring(2,4); da=parseInt(tstr,10);

tstr=text.substring(4,text.length); yr=parseInt(tstr,10);

if ( isNaN(yr) jj isNaN(mo) jj isNaN(da) jj mo<1 jj mo>12 jj da<1 jj da>31 )


da.toString() + "/" + yr.toString(); window.event.srcElement.value=text; bIsDate=true;


alert('Invalid Date');

return (bIsDate);

The purpose of the JavaScript function FormatDate shown in Listing 7-8 is to perform a cursory test for a valid date, regardless of whether the date has been entered using slashes. To gain access to the contents of the control, I use window.event.srcElement, which will be available because this function is called as an event handler.

If two slashes aren't found in the entered value, I try to interpret the entry as a date without slashes. If the string is six or eight characters, I split it into month, day, and year. To get a numeric value (for easier formatting later), I use the JavaScript function parseInt. One interesting feature of this function is that it interprets strings with leading zeros as octal by default. Thus, parseInt('09') would not be considered a number because there's no 9 in the octal number system. Thankfully, there's a second parameter, which accepts the base of the number. Calling parseInt('09',10) returns a correct value. If I've determined that the string entered without slashes is likely to be a date, I reformat it with slashes and update the value property of window.event.srcElement. If the date isn't valid, I use the alert function to display a message box on the client. All of this activity occurs without any intervention on the part of the client. Note How important is it to be able to change JavaScript files? In Beta 2

of ASP.NET, there was a bug in the JavaScript files. Fortunately, because the code wasn't baked into the controls, Microsoft was able to provide instructions that allowed developers to fix the problem themselves. In this example, the behavior of the date formatting could be changed independently of the control, allowing, for example, a date entered in ddmmyy format. Let's look back at Listing 7-7. Because ReformatDate is derived from BaseValidator, the ReformatDate control needs to implement a single function, EvaluatelsValid. My implementation is brief:

protected override bool EvaluateIsValid() {

this.ServerFormatDate(); return bIsValid;

EvaluatelsValid is a method that returns true if the form is valid or false if the form is not valid. ServerFormatDate, a method of ReformatDate, does much the same kind of checking that the FormatDate JavaScript function does. In general, if the browser is capable of using JavaScript, no invalid dates will get to the server, and so this function is used as a second line of defense. Caution In addition to acting as a validator of last resort, if the client browser doesn't support JavaScript, ServerFormatDate can also prevent users trying to enter or send invalid data. Remember, you have no control over exactly how the data gets to your server. All data posted from a client should be treated as suspect, unless proven otherwise! The ReformatDate control also implements two interfaces; IPostBackDataHandler and IPostBackEventHandler. If you need to have a control notified when data is posted back by the client, you must implement IPostBackEventHandler. Two methods must be implemented on this interface: LoadPostData and RaisePostDataChangedEvent. The implementation of these methods is shown here:

public virtual bool LoadPostData(string postDataKey, NameValueCollection values)

String presentValue = Text;

String postedValue = values[postDataKey];

if (!presentValue.Equals(postedValue)) {

Text = postedValue; return true;


return false;

public virtual void RaisePostDataChangedEvent() {

ServerFormatDate(); IsValid=bIsValid;


LoadPostData is passed in a postDataKey parameter as a string. postDataKey is used as a key into the second parameter, named values in this example—a NameValueCollection object. Using postDataKey, you can find the value for the current control and access it. In this example, I'm setting the Text property to the string retrieved from the NameValueCollection, if the value differs from the present value. The RaisePostDataChanged event is used in this example to call ServerFormatDate. ServerFormatDate sets the class variable bIsValid; doing so allows the control to signal that it isn't valid if the field returned can't be interpreted as a date.

The IPostBackEventHandler has a single method that must be implemented, RaisePostBackEvent. This event can be used to trigger an event whenever a postback occurs.

The OnTextChangedmethod calls the event handler TextChanged, if TextChanged is not set to null. TextChanged is an event handler that I've declared in this class. A client program could use this event to perform some action in the event that the text is changed. In many instances, declaring event handlers and calling them will allow consumers of your control to customize the control's behavior.

Was this article helpful?

0 0

Post a comment