mostlylucid

scott galloway's personal blog...
posts - 911, comments - 723, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Simple Guid validator...

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;

                                    }

?>

Print | posted on Monday, March 29, 2004 3:47 PM | Filed Under [ .NET Code Snippets ]

Feedback

Gravatar

# re: Simple Guid validator...

Woody likes it, So its good enough for me.

Peter
8/3/2004 9:14 PM | Peter Blackburn
Gravatar

# re: Simple Guid validator...

<Beavis>heheee he said woody.</Beavis>
9/3/2004 11:32 AM | Paul D. Murphy
Gravatar

# re: Simple Guid validator...

Well, Sounds like what I need as well. But still I prefer to write it myself instead of using other people's code. I am not sure it is reliable enought.
11/5/2004 1:30 PM | guoqi
Gravatar

# re: Simple Guid validator...

Umm...well if it helps...it's not my code either :-) In my own tests though this has had 0 false positives and 0 flase negatives out of a sample of 5 million entries with 50% as fails and 50 passes...
11/5/2004 1:34 PM | Scott Galloway
Comments have been closed on this topic.

Powered by: