Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 150, trackbacks - 155

My Links

News

Archives

Post Categories

Image Galleries

How to validate a valid GUID Value in C#

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.

Print | posted on Friday, May 20, 2005 5:20 AM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: How to validate a valid GUID Value in C#

Thank you very much for sharing your code. It has been a great help.
1/21/2006 7:17 AM | Frank Gennaro
Gravatar

# re: How to validate a valid GUID Value in C#

Microsoft's Regex stuff is really poorly documented, it seems. Thanks for this.
9/27/2006 1:54 PM | Pete
Gravatar

# re: How to validate a valid GUID Value in C#

Thanks for the great code!
1/23/2007 5:01 AM | Kirkov
Gravatar

# re: How to validate a valid GUID Value in C#

Thanks! This helped me. Not sure why they don't have a TryParse for Guids.
2/28/2007 10:10 PM | Mike
Gravatar

# re: How to validate a valid GUID Value in C#

Thank you so much. Exactly what I'm looking for
3/18/2007 10:58 PM | Frank
Gravatar

# re: How to validate a valid GUID Value in C#

fix the code for this kind of input
"{e73a048d- bf27-4f12-9731-8b2076e8891f"
4/12/2007 12:54 AM | Jagdish Vasani
Gravatar

# re: How to validate a valid GUID Value in C#

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"
4/12/2007 12:57 AM | Jagdish Vasani
Gravatar

# re: How to validate a valid GUID Value in C#

Why the hell my comment had a swastika icon next to it???
2/11/2008 5:35 AM | Ariel
Gravatar

# re: How to validate a valid GUID Value in C#

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???
2/21/2008 5:03 PM | K
Gravatar

# re: How to validate a valid GUID Value in C#

My thanks also for this code. I wonder what my graphic will look like?
3/18/2008 10:13 AM | Rory Becker
Gravatar

# re: How to validate a valid GUID Value in C#

Thanks for the code
4/12/2008 4:05 PM | D
Gravatar

# re: How to validate a valid GUID Value in C#

Good stuff, thanks!
6/27/2008 2:21 PM | Dmitriy K
Gravatar

# re: How to validate a valid GUID Value in C#

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.
9/23/2008 4:58 PM | Reza
Gravatar

# re: How to validate a valid GUID Value in C#

askl;ja awerj pojasdf kl;jaeiopuawr[iuoaer'kl asf'klasdfopw]tiotklj a;lksfa wpoeir adjklj;poiqwer
9/26/2008 4:55 PM | qwaer
Gravatar

# re: How to validate a valid GUID Value in C#

cool. I want i dyn image too :)
11/30/2008 6:16 PM | allegro
Gravatar

# re: How to validate a valid GUID Value in C#

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.
12/19/2008 9:58 AM | madsnakes
Gravatar

# re: How to validate a valid GUID Value in C#

Very nice, thanks for posting.
1/8/2009 6:12 PM | Mike
Gravatar

# re: How to validate a valid GUID Value in C#

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.
2/18/2009 8:44 AM | Neil Pullinger
Gravatar

# re: How to validate a valid GUID Value in C#

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".
2/18/2009 9:05 AM | Neil Pullinger
Gravatar

# re: How to validate a valid GUID Value in C#


I have a question and I know this is an OLD thread. However, if you have a variable that is already of type guid, your regex does not verify it is not an empty guid. An empty guid is also a properly formatted guid.

Do you have a regex that matches a valid guid that isn't an empty guid? I have modified one to find an empty guid, but I cannot seem to invert it.


Thank you
2/18/2009 9:54 AM | jason
Gravatar

# re: How to validate a valid GUID Value in C#

Thanx dude, you save me from so much work trying to figure this out myself!
2/26/2009 8:13 AM | Eben
Gravatar

# re: How to validate a valid GUID Value in C#

I wanna know how to create a GUID :-((((
4/28/2009 9:17 AM | Well
Gravatar

# re: How to validate a valid GUID Value in C#

Excellent! I can't believe they left this out
5/4/2009 5:24 AM | Jason
Gravatar

# re: How to validate a valid GUID Value in C#

to validate an empty guid just check for guid.empty.
5/27/2009 7:40 PM | Daniel
Gravatar

# re: How to validate a valid GUID Value in C#

//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).
8/25/2009 4:32 AM | =8)-DX
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: