Easily Create a Timestamped Filename in .NET

Eric says crazy filename parsing is something else you should stop doing.  If you’re creating output files, you probably need to timestamp the file name to keep the files separate.  Step away from the keyboard before you do anything crazy–you can do this in one line of VB.NET:

Dim _filename As String = String.Format(“MyFile.{0}.xml”, Now.ToString(“yyyyMMddHHmm”))


Don’t sniff browsers–sniff objects

As good as ASP.NET’s adaptive rendering is, I sometimes find myself needing to do some fairly substantial client-side scripting.  I used to sniff browsers, but that was getting tedious, and most common scripts are written upside down anyway.

The best method I’ve seen is to use ‘object sniffing’–testing browsers for support of different objects and methods.  This saves a considerable amount of time, since all browsers will fall into 3-4 categories of object support, and you don’t have to parse out a zillion different browser types and subtypes and still run the risk of excluding one that may work on your site.

My guide was http://developer.apple.com/internet/webcontent/objectdetection.html.  The article is a couple of years old, but it’s still useful to me today.

The pace of new browser releases may be slower than it was in the early days, but developers must still confront a bemusing array of browser versions and brands that support some JavaScript features but not others. To combat the problem, scripters commonly provide two or more code branches so that a browser follows an execution path containing statements that it supports. Browser sniffing — the task of inspecting navigator object properties for version information — has become largely unmanageable given the browser version permutations available today. This article presents details on an alternative solution — object detection — that frees JavaScript developers from most of this versioning mess.

I haven’t played around with custom server controls, so I don’t know how applicable this is for that purpose.

Allowing Only Numbers in ASP. NET TextBoxes – Use Regular Expression Validator!

[update 2012-07-16]

Kendo also has a numeric textbox input (both a widget and MVC extension), and Telerik has a numeric text input for ASP.NET Ajax.

[update 2011-10-07]

This continues to be a popular post, even though it was written in 2005.  If you need a solution for ASP.NET MVC, I’d suggest using a validation attribute on your model, and the Wijmo Complete Input jQuery UI widget (also available in the ASP.NET MVC Tools).

If you’re using WebForms, this method will still work, or you could look at the Wijmo-powered Input control.


This article came up on the news today: Allowing Only Numbers in ASP.NET Textboxes.  In this article, Bipin uses a custom validator to enforce input.  I usually like Bipin’s ideas, but this one seems to be a little more difficult than it needs to be.

I think there’s a better way to do this–use a regular expression validator.  To test this, create a page, and add a textbox, regular expression validator, and a button.  For the validation expression, enter the following:


Compile your page and test with some different inputs.

A quick explanation of the expression:

^ indicates the start of the input
[0-9] indicates a range of allowable characters.  You could do [0,1,2,3,4,5,6,7,8,9], but that’s not lazy.
+ indicates ‘match preceeding one or more times’
$ indicates the end of input

If you wanted to enforce a length of input, you could do something like:


which would enforce a minimum of 6 characters and a max of 12.

I think regular expressions are too often overlooked, but they’re very powerful and simple once you work with them a little bit.  For a good basic overview of regular expressions, check out:


The Web Professional’s Handbook


And a more in-depth RegEx reference in:


Pure JavaScript: 2nd Ed. (an absolute steal if you buy it used)


Regular Expressions with .NET [DOWNLOAD: PDF]

Downloadable e-bok in PDF format, from Amazon.


Mastering Regular Expressions, Second Edition (The Owl Book)