tags:

views:

1038

answers:

5

How can you check if a string is a valid GUID in vbscript? Has anyone written an IsGuid method?

+2  A: 

See Check a GUID.

fhe
I tried that one but I got a vbscript error. I'm guessing that's vb code not vbscript.
chumbawumba
+1  A: 

In VBScript you can use the RegExp object to match the string using regular expressions.

DaveK
+1  A: 

This is similar to the same question in c#. Here is the regex you will need...

^[A-Fa-f0-9]{32}$|^({|()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$

But that is just for starters. You will also have to verify that the various parts such as the date/time are within acceptable ranges. To get an idea of just how complex it is to test for a valid GUID, look at the source code for one of the Guid constructors.

pdavis
A: 

there is another solution:

try
{
  Guid g = new Guid(stringGuid);
  safeUseGuid(stringGuid); //this statement will execute only if guid is correct
}catch(Exception){}
A: 

This function might do the trick for you.

Function isGUID(byval strGUID)
  if isnull(strGUID) then
    isGUID = false
    exit function
  end if
  dim regEx
  set regEx = New RegExp
  regEx.Pattern = Replace("{########-####-####-####-############}", "#", "[0-9,A-F,a-f]")
  isGUID = regEx.Test(braces(strGUID))
  set RegEx = nothing
End Function
Techek