1
  1. Please be patient while reading the question as I am finding this one little hard to explain.
  2. Kindly suggest a better title so that it helps others in the community.

I have 3 fields. viz.

  1. Address Type - Dropdown(Values : 'Billing', 'Corporate')
  2. Effective Date - Datepicker (mm/dd/yyyy), mandatory
  3. Expiration Date - Datepicker (mm/dd/yyyy), not mandatory

I am in need to apply the following validation : Cannot have two addresses with the same type active at the same time.

Kindly refer below screenshot for examples.

enter image description here

Now, I have got 2 list(DateTime) viz. addressesEffectiveDatesList, addressesExpirationDatesList in C#. I am not getting a clue on how to achieve this.

Tinu Mathew
  • 195
  • 2
  • 10

2 Answers2

2

I have created a fiddle for you here

I would suggest you to do the client side validation as well. I have assumed your list of dates are in order, so ith element of start and end date would represent ith interval, so something in the line of

           //Checking to make sure all effective dates have end date, except the last one.
            for (var i = 1; i < effectiveEndDates.Count; i++)
            {
                if (!effectiveEndDates[i - 1].HasValue)
                {
                    result = "cannot have a previous billing address without and end date.";
                }
            }

            //Ensure all the startDates Dates are on or after the next end Date
            //This solution will work if the effective dates are in order.
            for (var i = 1; i < effectiveStartDates.Count; i++)
            {
                var effectiveEndDate = effectiveEndDates[i - 1];
                if (effectiveEndDate != null && effectiveEndDate.Value > effectiveStartDates[i])
                {
                    result = "cannot have 2 active billing address.";
                }
            }

would do the trick as long as the dates are in orders(as in effective dates). Hope this helps

update: I have updated the fiddle to ensure nothing overlaps with the infinite range. [Check this] (https://dotnetfiddle.net/nmyA3P)

SadiRubaiyet
  • 328
  • 2
  • 9
  • Hey @SadiRubaiyet, Thanks for the fiddle. Expiration dates are not mandatory. So, the first scenario which you wrote is a valid scenario. If Expiration Date is null and if there exists a second Effective Date then program should return 'Cannot have 2 active billing address' since for the first range, Expiration Date was null and that means the first range will never expire. So user should not be able to add another date range. Hope I could explain – Tinu Mathew Apr 26 '16 at 09:31
  • Yeah you could implement some client side logic as well which will allow you to do that easily. If the answer helped you please mark it accepted or helpful, glad to help – SadiRubaiyet Apr 26 '16 at 12:46
  • the answer is definitely helpful but can you please update the fiddle with the above mentioned case. – Tinu Mathew Apr 26 '16 at 13:23
  • let me explain the same once more. In my case 'To Dates' are NOT MANDATORY. For e.g. Range-1 From:-01/13/2015 To:- Range-1 From:-02/01/2015 To:- 02/20/2015 So, in the above example 'Range-1 To' is NULL which is allowed. It simply means, that 'Range-1 To' is infinite. So 'Range-2' overlaps. Can you help me in changing the logic to fit my case? – Tinu Mathew Apr 26 '16 at 13:40
  • updated my answer, but I was suggesting you that from the client side you would ensure the ranges are in order, so they will only get to input another range only after the endDate was assigned, that way it's consistent and you are not going back and forth with validation – SadiRubaiyet Apr 26 '16 at 14:24
  • yes you are absolutely right. But then, server side validation must be there. – Tinu Mathew Apr 26 '16 at 14:31
1

You'll need to check if the date ranges overlap. There's a good answer here: Determine Whether Two Date Ranges Overlap

Basically,

(StartA <= EndB) and (EndA >= StartB)

And you can compare DateTime objects with <= and >= in C#.

As SadiRubaiyet alluded to in the comment, it would make things quicker if you had each person's addresses ordered by start date because it means you only need to compare each date range with the very next one.

If they were not ordered, you would have to compare each date range to every other address for that person.

Community
  • 1
  • 1
Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84