Validate a GUID in C#

May 31, 2006

I recently needed to validate a GUID in C# and found this article on how to do it. I've simplified it to make it suit the problem I was trying to solve. Here's what I did with it:

private static Regex isGuid = 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}$", RegexOptions.Compiled);

private static bool IsGuid(string candidate)
{
   if (candidate != null)
   {
      if (isGuid.IsMatch(candidate))
      {
         return true;
      }
   }

   return false;
}

This method will validate any string and return true if it is a GUID.

* $4.95/month ASP.NET Hosting with FREE SQL 2012 DB! – Click Here!

Comments (4) -

Dan Atkinson
Dan Atkinson United Kingdom
8/19/2007 1:32:40 AM #

Why not extend the Guid class and write it thus:

public static class Guid
{
  private static Regex isGuid = 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}$", RegexOptions.Compiled);
  public static Boolean IsGuid(String guid)
  {
    if (guid != null)
    {
      if (isGuid.IsMatch(guid))
      {
        return true;
      }
    }
    return false;
  }
}

This way, you could reference the method using the following:

Boolean isValidGuid = Guid.IsGuid("3F2504E0-4F89-11D3-9A0C-0305E82C3301");

Not sure why I mentioned this. Maybe it's because it's nearly 4AM and I'm tired and can't really think logically.

Dan Atkinson
Dan Atkinson United Kingdom
8/19/2007 1:39:12 AM #

Yeah... Hmmm. It is late. Forgot about overwriting the existing Guid class, including its useful functions.

taras
taras Ukraine
12/24/2007 8:54:26 AM #

My preferred way to deal with Guid is to make it resemble it's value-type counterparts, such as Int32, Enum, etc, which all have <Type>.TryParse(..) in addition to <Type>.Parse(..).

It could lead to something like this:

public static class GuidHelper
{
_public static bool TryParse(object value, out Guid guidValue) {
__bool _result = false;
__try {
___guidValue = Guid.Parse(value);
____result = true;
__} catch {
___guidValue = Guid.Empty;
__}
__return _result;
_}
}

( This could look well more elegant with extension methods in C# 3.5, though i wonder why there's no such standard functionality in place, so one need to resort to exceptions :-\ )

Anuradha Jayasena
Anuradha Jayasena
6/5/2008 2:51:15 AM #

in that case this might be useful

private bool IsValidGuid(string strGuid)
        {

            bool returnValue = false;
            if (!string.IsNullOrEmpty(strGuid))
            {
                try
                {

                    Guid tmpGuid = new Guid(strGuid);
                    returnValue = true;

                }
                catch (Exception ex)
                {
                                    
                }              

            }

            return returnValue;
        }

I think try-catch is little bit heavy

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.