﻿# Simple Guid validator...

<datetime class="hidden">2004-05-19T00:00</datetime>
<!-- category -- mostlylucidcouk, Imported, C#, Software Development -->

Another of my fairly simple functions (actually written by a colleague of mine, Jonny Anderson) - this time just a simple way to validate a Guid - in this implementation  you pass in a Guid as an 'out' parameter along with the string you want to test - it then fills in the Guid and returns true / false depending on whether the Guid was valid... 

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);

internal static bool IsGuid(string candidate, out Guid output) 

{

bool isValid = false;

output=Guid.Empty;

if(candidate!=null)

{

if (isGuid.IsMatch(candidate)) 

{

output=new Guid(candidate);

isValid = true;

}

}

return isValid;

}
?>