AjmeraInfo
AjmeraInfo

Reputation: 504

How to validate Guid in .net

Please tell me how to validate GUID in .net and it is unique for always?

Upvotes: 13

Views: 31670

Answers (8)

Nick Craver
Nick Craver

Reputation: 630349

If you're looking for a way to determine if it's the format of the actual .Net Guid type, take a look at this article. A quick regex does the trick.

Upvotes: 0

Kyle Rosendo
Kyle Rosendo

Reputation: 25277

Guid's are unique 99.99999999999999999999999999999999% of the time.

It depends on what you mean by validate?

Code to determine that a Guid string is in fact a Guid, is as follows:

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;
}

Upvotes: 31

Chandu Ranwala
Chandu Ranwala

Reputation: 305

i wrote a extension for this

public static bool TryParseGuid(this Guid? guidString)
       {
           if (guidString != null && guidString != Guid.Empty)
           {
               if (Guid.TryParse(guidString.ToString(), out _))
               {
                   return true;
               }
               else
                   return false;
           }

           return false;
       }

Upvotes: 0

Damian Green
Damian Green

Reputation: 7495

In .net 4, you can use this extension method

public static class GuidExtensions
    {
        public static bool IsGuid(this string value)
        {
            Guid output;
            return Guid.TryParse(value, out output);
        }
    }

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941237

2^128 is a very, very large number. It is a billion times larger than the number of picoseconds in the life of the universe. Too large by a long shot to ever validate, the answer is doomed to be "42". Which is the point of using them: you don't have to. If you worry about getting duplicates then you worry for the wrong reason. The odds your machine will be destroyed by a meteor impact are considerably larger.

Duck!

Upvotes: 13

Wayne Bloss
Wayne Bloss

Reputation: 5550

Here's a non-Regex answer that should be pretty fast:

public static bool IsHex(this char c)
{
    return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}

public static bool IsGuid(this string s)
{
    // Length of a proper GUID, without any surrounding braces.
    const int len_without_braces = 36;

    // Delimiter for GUID data parts.
    const char delim = '-';

    // Delimiter positions.
    const int d_0 = 8;
    const int d_1 = 13;
    const int d_2 = 18;
    const int d_3 = 23;

    // Before Delimiter positions.
    const int bd_0 = 7;
    const int bd_1 = 12;
    const int bd_2 = 17;
    const int bd_3 = 22;

    if (s == null)
        return false;

    if (s.Length != len_without_braces)
        return false;

    if (s[d_0] != delim ||
        s[d_1] != delim ||
        s[d_2] != delim ||
        s[d_3] != delim)
        return false;

    for (int i = 0;
        i < s.Length;
        i = i + (i == bd_0 ||
                i == bd_1 ||
                i == bd_2 ||
                i == bd_3
                ? 2 : 1))
    {
        if (!IsHex(s[i])) return false;
    }

    return true;
}

Upvotes: 2

pierroz
pierroz

Reputation: 7870

this question was already discussed in this post. You may find more interesting details

Upvotes: 0

Kerido
Kerido

Reputation: 2940

You cannot validate GUID's uniqueness. You just hope it was generated with a tool that produces unique 16 bytes. As for validation, this simple code might work (assuming you are dealing with GUID's string representation:

bool ValidateGuid(string theGuid)
{
  try { Guid aG = new Guid(theGuid); }
  catch { return false; }

  return true;
}

Upvotes: 1

Related Questions