Wednesday, 2 July 2008

How do I validate a string Guid in C#.NET?

using System;
using System.Text.RegularExpressions;

public static class GuidUtility
{
public static bool IsGuid( string guid )
{
if ( !string.IsNullOrEmpty( guid ) )
{
Regex regex = new Regex( @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" );

return regex.IsMatch( guid );
}

return false;
}
}

No comments: