So here I am
writing another extension method for String to validate if sting value is a
valid Guid or not with the help of regular expression.
using System;
using System.Text.RegularExpressions;
public static
class StringExtensions
{
public static bool
IsValidGuid(this String
inputValue)
{
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);
bool
isValid = false;
if (!String.IsNullOrWhiteSpace(inputValue))
{
isValid =
isGuid.IsMatch(inputValue);
}
return
isValid;
}
}
|
And here is
how you can use this:
String testString = "6FE4851B-27A9-4D18-A51D-36B262023F39";
if (testString.IsValidGuid())
{
Response.Write("String
is a valid GUID");
}
else
{
Response.Write("String
is not a valid GUID");
}
|
Comments
Post a Comment