Tuesday, June 12, 2012

Better regular expression for phone numbers (multiple formats)

I'm often surprised at how often websites only accept a specific format for phone number input (U.S.) so I thought I would post a more flexible input format.

We want to allow users to enter phone numbers in formats like:

(123) 456-7890
123-456-7890
123.456.7890
1234567890

Using a regular expression, we can allow all these formats and more.


^\s*[(]?(?<AreaCode>\d{3})[)]?(\s*|[.-])(?<Prefix>\d{3})(\s*|[.-])?(?<LineNumber>\d{4})\s*$

Notice that we specify regular expression group names to easily pick up the phone number parts. In C#, this can be done like:

Code:
string prefix = match.Groups["Prefix"].Value;

Finally, here the some C# code to kick things off.

Code:
Regex rePhone = new Regex(@"^\s*[(]?(?<AreaCode>\d{3})[)]?(\s*|[.-])(?<Prefix>\d{3})(\s*|[.-])?(?<LineNumber>\d{4})\s*$");
Match match = rePhone.Match("123.456.7890");
Console.WriteLine(match.Success);

Monday, June 11, 2012

Monday, May 21, 2012

Ever want to see SQL JOIN statements visually? Let's assume we have two tables, Person and Pet.

Click on the image below for a full view.

Tuesday, April 10, 2012

Monday, April 2, 2012

Nice cheat sheet on many HTML5 and CSS3 features

Here is a nice cheat sheet for some of the more requested items related to HTML5 and CSS3.  Not too bad.

Stories In Flight

Sunday, April 1, 2012

ASP.NET generic content box with title and body

Here is a quick example of creating a simple ASP.NET web control that allow you to have a region/box that contains a title and body where each of three elements can each have their own CSS class.

You can use the content box in a web page like:

Code:
using System.Web.UI.WebControls;

namespace CarverPainting
{
   public class ContentBox : Panel
   {
      public string CssClassBody
      {
         get { return base.CssClass; }
         set { base.CssClass = value; }
      }

      public override string CssClass
      {
         get
         {
            return ViewState["CssClassMain"] == null ? string.Empty : ViewState["CssClassMain"].ToString();
         }

         set
         {
            ViewState["CssClassMain"] = value;
         }
      }

      public string CssClassTitle
      {
         get
         {
            return ViewState["CssClassTitle"] == null ? string.Empty : ViewState["CssClassTitle"].ToString();
         }

         set
         {
            ViewState["CssClassTitle"] = value;
         }
      }

      public string Title
      {
         get
         {
            return ViewState["Title"] == null ? string.Empty : ViewState["Title"].ToString();
         }

         set
         {
            ViewState["Title"] = value;
         }
      }

      protected override void Render(System.Web.UIHtmlTextWriter writer)
      {
         bool hasClass = !string.IsNullOrWhiteSpace(CssClass);
         bool hasClassTitle = !string.IsNullOrWhiteSpace(CssClassTitle);
         bool hasClassBody = !string.IsNullOrWhiteSpace(CssClassBody);

         // contentbox class
         writer.Write("<div{0}>", hasClass ? string.Format(" class=\"{0}\"", CssClass) : string.Empty);

         if (!string.IsNullOrWhiteSpace(Title))
         {
            writer.Write("<div{0}>{1}</div>", hasClassTitle ? string.Format(" class=\"{0}\"", CssClassTitle) : string.Empty, Title);
         }

         base.Render(writer);

         // contentbox class
         writer.Write("</div>");
      }
   }
}

Using dotlesscss with Visual Studio and IIS

I've started to use LESS the dynamic stylesheet language with Microsoft's ASP.NET.  For starters, it really does help write and maintain stylesheets.  The .NET version can be downloaded at http://www.dotlesscss.org/.

I did however run into an issue when I deployed my development code to a production server.  It turns out that the Visual Studio 2010 web server handles the dotlesscss DLL differently from IIS 7.5.  In a nutshell, to use dotlesscss in both environments (local, production) without having to made changes to one of the environments the setup I use is:
  1. Set my web project to use IIS Express (separate download from Microsoft).
  2. Add the following items to my web.config file
xml version="1.0" encoding="utf-8"?>

<configuration>
   <configSections>
      <section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler,dotless.Core" />
   configSections>
   <system.web>
      <compilation debug="true" targetFramework="4.0" />
      <pages>
         <controls>
            <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
         controls>
      pages>
   system.web>
   <system.webServer>
      <handlers>
         <add name="dotless" type="dotless.Core.LessCssHttpHandler,dotless.Core" path="*.less.css" verb="*"/>
      handlers>
   system.webServer>
   <dotless minifyCss="false" cache="false" />
configuration>


Wednesday, February 29, 2012

Why does copy/paste not work with Remote Desktop

So what do I do when clipboard stops working when you are using Windows Remote Desktop?

Fixing the issue is pretty straightforward and involves a few basic steps.
  1. Load up task manager (right click taskbar and select Task Manager)
  2. Go to the Processes Tab
  3. Select rdpclip.exe
  4. Click End Process
  5. Go to the Application Tab
  6. Click New Task
  7. Type rdpclip
  8. Click OK

That should get things going again.

Sunday, May 15, 2011

MVC 3, Razor, and Entity Framework Code-First

Here is a good tutorial (and downloadable PDF) on using MVC 3, Razor, and the EF Code-First (Code-First CPT 5 Release).

Visit Tutorial

I think the best part about the tutorial is how Code-First makes it really simple to set up pure POCO objects for data access.

Friday, January 21, 2011

Visual Studio 2010 Find/Replace Dialog Growing

For anyone interested in fixing their ever growing find dialog in Visual Studio 2010, there’s now a patch from Microsoft:

Download Growing Find and Replace Dialog

Saturday, January 1, 2011

Use ApplicationException when creating your own custom exceptions

In March of 2009 I talked about Catch (Exception e): Right or wrong?. Since that time I've gotten into the habit of also deriving my custom application exceptions from the .NET ApplicationException rather than just Exception. This .NET class serves to help identify exceptions that are specific to an application, hence the name.

To make things really easy for us to create application-specific exceptions, use the Visual Studio 2010 code snippet, "exception". Be sure to use the tab key to update the base class to ApplicationException. The snippet contains all the code to adhere to the exception derivation best practices.

Very handy!

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...