Wednesday, 30 October 2013

How to validate Guid string in c#

Description:

This article contains the C# code snippets to validate Guid string in C# with Regular Expression and without Regular Expression.

Summary:

  1. Validate Guid string in .NET C# with Regular Expression
  2. Check and Validate Guid string in .NET C# without Regular Expression

Validate Guid string in .NET C# with Regular Expression

You can use this C# function to check whether the given Guid string is in valid Guid format or not.
public static bool IsGuid1(string guidString)
    {
        bool isValid = false;
        if (!string.IsNullOrEmpty(guidString))
        {
            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);

            if (isGuid.IsMatch(guidString))
            {
                isValid = true;
            }
        }
        return isValid;
    }

Check and Validate Guid string in .NET C# without Regular Expression

You can use this C# function to check whether the given Guid string is in valid Guid format or not without using Regular Expression which considers to be a fast method.
 public static bool IsGuid(string guidString)
    {
        // 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 (guidString == null)
            return false;

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

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

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

        return true;
    }

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

Thanks,
Morgan
Software Developer

Advertisements
Advertisements

2 comments:

  1. Howԁ&X79;, i rеad your blog from time to time and i ow&X6E; a similar оnе and i was just
    wondering if you g&X65;t a lot of ѕpa&X6d; commen&X74;ѕ?
    If so how ԁo you reduce it, any plugin o&X72; anythіn&X67; you cann ѕu&X67;geѕt?
    I ge&X74; so mu&X63;h lаtely it's d&X72;iving me in&X73;ane so any help is very much appreciated.


    Аlso visit my page - belstaff racemaster

    ReplyDelete
  2. Hel&X6C;o, i гeaԁ уouг blog from &X74;ime tο ti&X6d;е and
    i own a &X73;i&X6D;іlaг one and i was just curiouѕ іf
    you get a lot of spam re&X6D;arks? If so how do
    you proteс&X74; agaі&X6E;st іt, any plugin or аnythin&X67; you c&X61;&X6E;
    advіѕe? Ι get so &X6D;uc&X68; lately it's
    ԁгiving m&X65; іnsane so any helρ iѕ very muc&X68;
    а&X70;prеciat&X65;d.

    my web-sitе ... maillot football pas cher

    ReplyDelete