How do you test your Request.QueryString[] variables?

Below is an extension method that will allow you to write code like this: int id = request.QueryString.GetValue<int>(“id”); DateTime date = request.QueryString.GetValue<DateTime>(“date”); It makes use of TypeDescriptor to perform the conversion. Based on your needs, you could add an overload which takes a default value instead of throwing an exception: public static T GetValue<T>(this NameValueCollection … Read more

jQuery: what is the best way to restrict “number”-only input for textboxes? (allow decimal points)

If you want to restrict input (as opposed to validation), you could work with the key events. something like this: <input type=”text” class=”numbersOnly” value=”” /> And: jQuery(‘.numbersOnly’).keyup(function () { this.value = this.value.replace(/[^0-9\.]/g,”); }); This immediately lets the user know that they can’t enter alpha characters, etc. rather than later during the validation phase. You’ll still … Read more

Identify if a string is a number

int n; bool isNumeric = int.TryParse(“123”, out n); Update As of C# 7: var isNumeric = int.TryParse(“123”, out int n); or if you don’t need the number you can discard the out parameter var isNumeric = int.TryParse(“123”, out _); The var s can be replaced by their respective types!