Inaugural Post
Hi, My name is Chris McKenzie. I'll be blogging from time to time about my experiences and discoveries regarding .NET programming. My hope is to document the things I learn as I go, and to provide a forum for constructive feedback from other developers.
I guess I'll jump right in. I'm developing an ASP .NET server control that will accept a report path to SQL Server Reporting Services (RS) and dynamically render UI for the report's parameters. The control consists of 5 pieces: a containing table, a label for the user prompt, a value control (meaning, textbox, dropdown, radiobuttons, checkboxes, etc.), a required field validator, and a datatype validator.
For the most part, this is a cut-and-dry composite server control. However, something wierd happens with the RS DateTime datatype. The datatype validation for the CompareValidator is limited to the Date (MM/dd/yyyy) only, and RS will only accept a parameter in DateTime (MM/dd/yyyy hh:mm:ss AM/PM) format. However, I want to allow the user to optionally enter the time. The control should be smart enough to append 12:00:00 AM to MM/dd/yyyy. This means I have to provide a CustomValidator with a custom regular expression for the DateTime datatype.
This regex works well, for making sure that everything is in the right format:
([0]?[1-9]|[1][0-2])[-/]([0]?[1-9]|[1-2]\d{1}|[3][0-1])[-/][1-2]\d{3}(\s([0]?[1-9]|[1][1-2]):[0-5]\d{1}(:[0-5]\d{1})?\s[AP][M])?$
Basically, I'll accept (M or MM)/(d or dd)/yyyy or (M or MM)/(d or dd)/yyyy (h or hh):mm(” “ or :ss) (A or P)M. But I'm still not done, because I want the final figure in a standard format. In other words, I want 1/1/2004 5:30 PM to return as “01/01/2004 05:30:00 PM.” I can use this javascript to handle the date portion:
s = s.replace(/\b\d{1}[-/:]/g, "0$&");
// // replace M/d/yyyy h:mm with MM/dd/yyyy hh:mm
Now we're getting closer to my problem. I'm trying to do the time portion. If time is specified, I want to append “:00” to hh:mm, but only if :ss is not specified. I experimented using Roy Osherove's excellent Regulator application and came up with:
// \d{2}:\d{2}(?=\s)(?<=\s\d{2}:\d{2})
regExStr = "\\d{2}:\\d{2}"; // where pattern matches ##:##
regExStr += "(?=\\s)"; // and the next character is a space
regExStr += "(?<=\\s\\d{2}:\\d{2})"; // and the preceding characters match " ##:##" (i.e., not ##:##:##)
r = new RegExp(regExStr);
s = s.replace(r, "$&:00 "); // replace hh:mm with hh:mm:ss
When I ran the code, imagine my surprise when I got an error basically stating that regExStr was invalid. So, I checked the Regulator again, and everything seemed fine. Then I checked some only javascript regex testers, and found that the (?=...) and all variants created the same error.
I went ahead and coded a workaround, but I'm left with a nagging curiosity: is this documented? Does the javascript RegExp() object not support sub expressions? Or is this an IE only phenomenon? I did a quick google about it, and couldn't find anything. Does anyone else have answers to these questions?