Geeks With Blogs
Jawad Khan Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.

VIA Scott Galloway's Blog

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;

                                    }

This is very useful in other Microsoft Products like Microsof Content Management Server/SharePoint  where you use GUID quite frequently in the API Calls.

Posted on Friday, May 20, 2005 5:20 AM ASP.NET | Back to top


Comments on this post: How to validate a valid GUID Value in C#

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Thank you very much for sharing your code. It has been a great help.
Left by Frank Gennaro on Jan 21, 2006 7:17 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Microsoft's Regex stuff is really poorly documented, it seems. Thanks for this.
Left by Pete on Sep 27, 2006 1:54 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Thanks for the great code!
Left by Kirkov on Jan 23, 2007 5:01 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Thank you so much. Exactly what I'm looking for
Left by Frank on Mar 18, 2007 10:58 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
fix the code for this kind of input
"{e73a048d- bf27-4f12-9731-8b2076e8891f"
Left by Jagdish Vasani on Apr 12, 2007 12:54 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
sry there was an extra space after the 1st dash in my earlier post -->

fix the code for this kind of input
"{e73a048d- bf27-4f12-9731-8b2076e8891f"

its should be
"{e73a048d-bf27-4f12-9731-8b2076e8891f"
Left by Jagdish Vasani on Apr 12, 2007 12:57 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Why the hell my comment had a swastika icon next to it???
Left by Ariel on Feb 11, 2008 5:35 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
It's probably an auto-generated ip address to icon. Every ip has his unique icon.

Funny though

>Why the hell my comment had a swastika icon next to it???
Left by K on Feb 21, 2008 5:03 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
My thanks also for this code. I wonder what my graphic will look like?
Left by Rory Becker on Mar 18, 2008 10:13 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Thanks for the code
Left by D on Apr 12, 2008 4:05 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Good stuff, thanks!
Left by Dmitriy K on Jun 27, 2008 2:21 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Thank you so much,
There is another silliy solution without RegEx for that.

public static bool IsGuid(string guidString)
{
bool bResult=false;
try
{
Guid g = new Guid(guidString);
bResult=true;
}
catch
{
bResult=false;
}

return bResult;
}

If the guidString is not valid, Guid class throws an error.
Left by Reza on Sep 23, 2008 4:58 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
cool. I want i dyn image too :)
Left by allegro on Nov 30, 2008 6:16 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
This is great though it is doing two things 1. Validating 2. Converting string to Guid. I would rather do just the check which removes the coupling:

private static readonly Regex guidPattern = 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 bool IsGuid(this string input)
{
return guidPattern.IsMatch(input);
}

then create the guid instance as needed, SRP and all that.
Left by madsnakes on Dec 19, 2008 9:58 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
This code has a bug, allowing unmatched curly braces.

You can pass in a string like:

{1102F615-9508-41EF-9699-B517C09D5FEE

and the method will return true.
Left by Neil Pullinger on Feb 18, 2009 8:44 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Further to my comment, you could change the regex to enforce the use of curly braces. Or change it not to have curly braces at all. You could pass in an extra parameter to the method to to switch between "must have both braces" or "must not have any braces".
Left by Neil Pullinger on Feb 18, 2009 9:05 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Thanx dude, you save me from so much work trying to figure this out myself!
Left by Eben on Feb 26, 2009 8:13 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
I wanna know how to create a GUID :-((((
Left by Well on Apr 28, 2009 9:17 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
to validate an empty guid just check for guid.empty.
Left by Daniel on May 27, 2009 7:40 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
//Well, This is how you generate a new Guid:
Guid mynewguid = Guid.NewGuid();

I think the try-catch method is better, because there are various valid Guid string formats, see: <quote>A String that contains a GUID in one of the following formats ('d' represents a hexadecimal digit whose case is ignored): 32 contiguous digits: dddddddddddddddddddddddddddddddd -or- Groups of 8, 4, 4, 4, and 12 digits with hyphens between the groups. The entire GUID can optionally be enclosed in matching braces or parentheses: dddddddd-dddd-dddd-dddd-dddddddddddd -or- {dddddddd-dddd-dddd-dddd-dddddddddddd} -or- (dddddddd-dddd-dddd-dddd-dddddddddddd) -or- Groups of 8, 4, and 4 digits, and a subset of eight groups of 2 digits, with each group prefixed by "0x" or "0X", and separated by commas. The entire GUID, as well as the subset, is enclosed in matching braces: {0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}} All braces, commas, and "0x" prefixes are required. All embedded spaces are ignored. All leading zeroes in a group are ignored.The digits shown in a group are the maximum number of meaningful digits that can appear in that group. You can specify from 1 to the number of digits shown for a group. The specified digits are assumed to be the low order digits of the group. </quote>

When you look at the Guid constructer code it doesn't use regex. The correct regex would have to match all valid guid formats or at least not match any bad guids

Also when catching the exception it is best to also catch OverflowException (as well as FormatException).
Left by =8)-DX on Aug 25, 2009 4:32 AM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
2 comments, the regex allows unmatched curly braces. And as far as the try catch I hope you're not expecting any kind of performance from that code. Running a regex is much faster then throwing/catching an exception. Yes it's easier, but that doesn't always make it better.
Left by Justin on Jan 22, 2010 6:15 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
>> Running a regex is much faster then throwing/catching an exception.

Depends. If you're getting mostly pass results, and only rare fails, then the regex is slower, because the majority of the overhead of a try/catch only happens when the catch is actually invoked.
Left by Jadawin on Feb 11, 2010 4:05 PM

# re: How to validate a valid GUID Value in C#
Requesting Gravatar...
Another solution:
Guid g;
return Guid.TryParse(guidString, out g);
Left by Pieter on May 31, 2011 3:41 AM

Your comment:
 (will show your gravatar)
 


Copyright © Jawad Khan | Powered by: GeeksWithBlogs.net | Join free