0

I searched google and SO for my scenario but not able to find an answer. I want to create a regular expression data annotation validation in a viewmodel class properties which are of double type. Since I have around 20 properties of type double. So I want to create a custom regular expression validation and apply to all double type properties without explicitly specifying on each property like:

[RegularExpression(@"^[0-9]{1,6}(\.[0-9]{1,2})?$", ErrorMessage ="Invalid Input")]
public double Balance { get; set; }

I am expecting thing like this:

[ApplyRegExpToAllDoubleTypes]
public class MyModel
{
     public double Balance { get; set; }
     public double InstallmentsDue { get; set; }
}
Mahesh Kudikala
  • 187
  • 1
  • 6
  • 1
    i don't think that is possible... – jazb Aug 02 '19 at 08:07
  • Ehrm... `double` is a number. Regular expressions are for strings. How does this work out for you? Your regex is validating that the input is a number with decimal point. But having `double` type of the property already does that for you - the framework does the checking. Why do you need regex on a double? – trailmax Aug 03 '19 at 22:24
  • Also, are you sure you want `double` on `Balance` variable? I feel this is about money. And money are better represented by `decimal`: https://stackoverflow.com/a/1165788/809357 – trailmax Aug 03 '19 at 22:25

1 Answers1

-1

That's an interesting question. Here is how it can be done:

Define a custom ValidationAttribute and apply it at the class level by setting AttributeTargets.Class. Inside the ValidationAttribute, use reflection to get the double properties, then validate the value of each property. If any of the validations fail, return a validation failed message.

[ApplyRegExpToAllDoubleTypes]
public class MyModel {
    public double Balance { get; set; }
    public double InstallmentsDue { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class ApplyRegExpToAllDoubleTypes : ValidationAttribute {
    protected override ValidationResult IsValid(object currentObject, ValidationContext validationContext) {
        if (currentObject == null) {
            return new ValidationResult("Object can't be null");
        }

        var properties = validationContext.ObjectType.GetProperties().Where(x => x.PropertyType == typeof(double));
        foreach (var property in properties) {
            //Here I compare the double property value against '5'
            //Replace the following with the custom regex check
            if ((double)property.GetValue(currentObject) < 5) {
                return new ValidationResult("All double properties must be greater than 5");
            }
        }
        return ValidationResult.Success;
    }
}
rhytonix
  • 968
  • 6
  • 14
  • Yes, you apply a validation to all properties of type. But the question ask about regex validation on double (which makes no sense). – trailmax Aug 03 '19 at 22:28
  • @trailmax the question is about applying validation to multiple properties without having to annotate every single property in the class. I didn't include OP's regex in my answer because it's irrelevant to the main point of the question (for all I care about the given regex could be invalid regex). And in general RegularExpression can be used to set some validation rules on a double type, so I'm not sure what your point is. – rhytonix Aug 04 '19 at 14:55