C# 字符串字段的货币范围和格式验证器

C# 字符串字段的货币范围和格式验证器,c#,data-annotations,C#,Data Annotations,以下情况下的正确货币数据注释是什么 小数点后两位的数字金额 最低金额1.00美元;最高金额25000.00美元 这是我的领域 public string amount {get; set;} 请使用后面的Culture info类检查小数位。您可以使用RegularExpression注释和适当的正则表达式来匹配您的数字格式: [RegularExpression(@"(\.\d{2}){1}$")] 要检查最小值和最大值,我们必须创建自己的自定义属性 [MinDecimalValue(1.

以下情况下的正确货币数据注释是什么

  • 小数点后两位的数字金额
  • 最低金额1.00美元;最高金额25000.00美元
  • 这是我的领域

    public string amount {get; set;}
    

    请使用

    后面的Culture info类检查小数位。您可以使用RegularExpression注释和适当的正则表达式来匹配您的数字格式:

    [RegularExpression(@"(\.\d{2}){1}$")]
    
    要检查最小值和最大值,我们必须创建自己的自定义属性

    [MinDecimalValue(1.00)]
    [MaxDecimalValue(25000.00)]
    public string amount { get; set; }
    
    我们可以通过创建一个派生自ValidationAttribute的类并重写IsValid方法来实现这一点

    请参阅下面我的实现

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
        public class MaxDecimalValueAttribute : ValidationAttribute
        {
            private double maximum;
            public MaxDecimalValueAttribute(double maxVal)
                : base("The given value is more than the maximum allowed.")
            {
                maximum = maxVal;
            }
            public override bool IsValid(object value)
            {
                var stringValue = value as string;
                double numericValue;
                if(stringValue == null)
                    return false;
                else if(!Double.TryParse(stringValue, out numericValue) ||  numericValue > maximum)
                {
                    return false;
                }
            return true;
        }
    }
    
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class MinDecimalValueAttribute : ValidationAttribute
    {
        private double minimum;
        public MinDecimalValueAttribute(double minVal)
            : base("The given value is less than the minimum allowed.")
        {
            minimum = minVal;
        }
        public override bool IsValid(object value)
        {
            var stringValue = value as string;
            double numericValue;
            if (stringValue == null)
                return false;
            else if (!Double.TryParse(stringValue, out numericValue) || numericValue < minimum)
            {
                return false;
            }
    
            return true;
        }
    }
    
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,AllowMultiple=false,Inherited=true)]
    公共类MaxDecimalValueAttribute:ValidationAttribute
    {
    私人双倍最大值;
    公共MaxDecimalValueAttribute(双maxVal)
    :base(“给定值大于允许的最大值。”)
    {
    最大值=最大值;
    }
    公共覆盖布尔值有效(对象值)
    {
    var stringValue=作为字符串的值;
    双数值;
    if(stringValue==null)
    返回false;
    如果(!Double.TryParse(stringValue,out numericValue)| | numericValue>最大值)
    {
    返回false;
    }
    返回true;
    }
    }
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,AllowMultiple=false,Inherited=true)]
    公共类MinDecimalValueAttribute:ValidationAttribute
    {
    私人双最小值;
    public MinDecimalValueAttribute(双最小值)
    :base(“给定值小于允许的最小值。”)
    {
    最小值=最小值;
    }
    公共覆盖布尔值有效(对象值)
    {
    var stringValue=作为字符串的值;
    双数值;
    if(stringValue==null)
    返回false;
    否则如果(!Double.TryParse(stringValue,out numericValue)| | numericValue<最小值)
    {
    返回false;
    }
    返回true;
    }
    }
    
    您可以阅读更多关于如何创建自己的属性的内容,这段代码也需要进一步改进


    希望这有帮助

    这会考虑到价值前面的美元吗?我忘了。您可以修改正则表达式注释来处理该问题。