Tuesday, May 27, 2008

GUID Validator

Working with Unique Identifier (known also as GUID) I need to know if a funtion gived or returned string is really a GUID or something else. Since there isn't a predefined funtion to do that, I create my own function using Regular Expressions:


Public Function IsGUID(ByVal guid As String) As Boolean
    Dim rtn As Boolean = False
    If Not String.IsNullOrEmpty(guid) Then
        Dim guidRegEx As Regex = 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})$")
        rtn = guidRegEx.IsMatch(guid)
    Else
        rtn = False
    End If

    Return rtn
End Function

No comments: