locked
Enterprise Library Validator to allow both NULL and Reg Format RRS feed

  • Question

  • User619554136 posted
    Class Data
    {
    [RegexValidator(@"^\d{1,3}\.\d{1,2}$|^\d{1,3}$", MessageTemplate = "Invalid, Correct Format 999.99"]
    public Decimal? Amount { get; set; }
    }

    Amount should allow only either NULL or the format 999.99

    Here Amount field is Nullable Decimal. Will EL.validate() method allow amount to be having NULL, or it simply check the reg format.

    if EL.Validate() method does not allow NULL, then How to specify the Validator attribute to allow either NULL or the format 999.99?

    Wednesday, March 16, 2011 12:14 AM

Answers

  • User619554136 posted

    I spent some time on this problem. It has two issues.

    Issue1: RegexValidator can not be applied on Decimal. So thought to use RangeValidator,

    So thought to use RangeValidator

    Issue2: but RangeValidator does not support with Decimals. It works for only double, float, int, long.

    Following is the solution for these problems, it allows NULL and specific range on Decimals.

    Please observe typeof(decimal) in below snippet.

        public class Data
    {
    [IgnoreNulls]
    [RangeValidator(typeof(decimal), "0.00", RangeBoundaryType.Inclusive, "9999999.99", RangeBoundaryType.Inclusive)]
    public Decimal? FederalTaxExemptionAmount { get; set; }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 16, 2011 4:40 AM