Monthly Archives: October 2016

Common Csharp dotnet Format specifiers or Format Strings for display

Name Format String Example Comments Currency Format C or c or C2 – precision Console.WriteLine(amount.ToString(“C”)) output depends on your current UI culture Percent Format P or p or P2 – precision) Console.WriteLine(number.ToString(“P”)) output depends on your current UI culture Padding with leading zero Format 0 var input = “123”; Console.WriteLine(input.ToString(“0000”)); //result will be 0123 Replaces… Read More »

Common Csharp dotnet Regex expressions for user input validation

Name Pattern Example US zip code regex ^d{5}(?:[-s]d{4})?$ 12345 or 12345-2345 Canadian zip code regex ^([ABCEGHJKLMNPRSTVXY]d[ABCEGHJKLMNPRSTVWXYZ]) {0,1}(d[ABCEGHJKLMNPRSTVWXYZ]d)$ M4B 1B4 US phone number regex (?d{3})?-? *d{3}-? *-?d{4} (123) 345-234 US Date (MM/dd/YYYY) regex ^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/]d{4}$ 05/31/2016 24 Hour time regex ^(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$ 13:12 12 Hour time regex (AM/PM) ^(([0]?[0-9]|1[0-2]):[0-5][0-9][ ][aApP][mM])|((1[3-9]|2[0-3]):[0-5][0-9])$ 7:00 AM Url regex ^(https?://)?([da-z.-]+).([a-z.]{2,6})([/w .-]*)*/?$ http://www.test.com or… Read More »