如何验证Guid数据类型?

时间:2022-09-15 19:47:25

Is there a way to validate GUID datatype?

有没有办法验证GUID数据类型?

I'm using validation attributes. http://msdn.microsoft.com/en-us/library/ee707335%28v=vs.91%29.aspx

我正在使用验证属性。 http://msdn.microsoft.com/en-us/library/ee707335%28v=vs.91%29.aspx

4 个解决方案

#1


6  

You can use a RegularExpressionAttribute. Here's a sample using the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:

您可以使用RegularExpressionAttribute。以下是使用格式xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx的示例:

[RegularExpression(Pattern = "[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}")]

You can also create a custom validation attribute, which is probably a cleaner solution.

您还可以创建自定义验证属性,这可能是一个更清洁的解决方案。

#2


3  

You could write your own subclass of CustomValidationAttribute that ensures the value is a guid by using the TryParse method of System.Guid (thanks Jon!).

您可以编写自己的CustomValidationAttribute子类,通过使用System.Guid的TryParse方法确保值是一个guid(感谢Jon!)。

#3


0  

This function might help you....

这个功能可能对你有帮助....

public static bool IsGUID(string expression)
{
    if (expression != null)
    {
        Regex guidRegEx = 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})$");

        return guidRegEx.IsMatch(expression);
    }
    return false;
}

You may remove the static or put the function in some Utility Class

您可以删除静态或将函数放在某个实用程序类中

#4


0  

This will use .Net's built-in Guid type for the validation, so you don't have to use a custom regular expression (which hasn't undergone Microsoft's rigorous testing):

这将使用.Net的内置Guid类型进行验证,因此您不必使用自定义正则表达式(未经过Microsoft的严格测试):

public class RequiredGuidAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        var guid = CastToGuidOrDefault(value);

        return !Equals(guid, default(Guid));
    }

    private static Guid CastToGuidOrDefault(object value)
    {
        try
        {
            return (Guid) value;
        }
        catch (Exception e)
        {
            if (e is InvalidCastException || e is NullReferenceException) return default(Guid);
            throw;
        }
    }
}

You then just use it like this:

然后你就像这样使用它:

    [RequiredGuid]
    public Guid SomeId { get; set; }

If any of the following are provided to this field, it will end up as a default(Guid), and will be caught by the Validator:

如果向此字段提供以下任何内容,它将最终作为默认值(Guid),并将被Validator捕获:

{someId:''}
{someId:'00000000-0000-0000-0000-000000000000'}
{someId:'XXX5B4C1-17DF-E511-9844-DC4A3E5F7697'}
{someMispelledId:'E735B4C1-17DF-E511-9844-DC4A3E5F7697'}
new Guid()
null //Possible when the Attribute is used on another type
SomeOtherType //Possible when the Attribute is used on another type

#1


6  

You can use a RegularExpressionAttribute. Here's a sample using the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:

您可以使用RegularExpressionAttribute。以下是使用格式xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx的示例:

[RegularExpression(Pattern = "[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}")]

You can also create a custom validation attribute, which is probably a cleaner solution.

您还可以创建自定义验证属性,这可能是一个更清洁的解决方案。

#2


3  

You could write your own subclass of CustomValidationAttribute that ensures the value is a guid by using the TryParse method of System.Guid (thanks Jon!).

您可以编写自己的CustomValidationAttribute子类,通过使用System.Guid的TryParse方法确保值是一个guid(感谢Jon!)。

#3


0  

This function might help you....

这个功能可能对你有帮助....

public static bool IsGUID(string expression)
{
    if (expression != null)
    {
        Regex guidRegEx = 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})$");

        return guidRegEx.IsMatch(expression);
    }
    return false;
}

You may remove the static or put the function in some Utility Class

您可以删除静态或将函数放在某个实用程序类中

#4


0  

This will use .Net's built-in Guid type for the validation, so you don't have to use a custom regular expression (which hasn't undergone Microsoft's rigorous testing):

这将使用.Net的内置Guid类型进行验证,因此您不必使用自定义正则表达式(未经过Microsoft的严格测试):

public class RequiredGuidAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        var guid = CastToGuidOrDefault(value);

        return !Equals(guid, default(Guid));
    }

    private static Guid CastToGuidOrDefault(object value)
    {
        try
        {
            return (Guid) value;
        }
        catch (Exception e)
        {
            if (e is InvalidCastException || e is NullReferenceException) return default(Guid);
            throw;
        }
    }
}

You then just use it like this:

然后你就像这样使用它:

    [RequiredGuid]
    public Guid SomeId { get; set; }

If any of the following are provided to this field, it will end up as a default(Guid), and will be caught by the Validator:

如果向此字段提供以下任何内容,它将最终作为默认值(Guid),并将被Validator捕获:

{someId:''}
{someId:'00000000-0000-0000-0000-000000000000'}
{someId:'XXX5B4C1-17DF-E511-9844-DC4A3E5F7697'}
{someMispelledId:'E735B4C1-17DF-E511-9844-DC4A3E5F7697'}
new Guid()
null //Possible when the Attribute is used on another type
SomeOtherType //Possible when the Attribute is used on another type