3

Is there any way to use @Range to validate a range of dates in Regula? (ditto @Min and @Max)

Or do I need to use @Custom?

@Range(min=

and

@Range(max=

do not seem to accept anything of the type Date - only numbers or strings.

sq33G
  • 3,320
  • 1
  • 23
  • 38

2 Answers2

1

Unfortunately @Range only accepts numbers. I think you can do something like this though:

<input type="hidden" 
       name="date" 
       id="date" 
       data-constraints="
           @Future(date='2000/1/1', format='YMD') 
           @Past(date='2010/1/1', format='YMD')" 
/>

This ensures that the date is after 2000/1/1 and before 2010/1/1 (i.e., in between). I didn't document the date parameter because I don't think I had implemented it when I wrote the documentation. Sorry; the documentation is a bit behind because I'm working on rolling version 1.3 of Regula out, that will have a lot more goodies. I'll be getting started on updating the documentation soon!

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • Thanks! Docs are good, new v. with goodies sounds good too! For the record, using a string seems to work - but since I was using Unix timestamps, there was really no reason for it not to be integer values. – sq33G Sep 01 '13 at 15:26
  • `@Range` should work if you were using epoch time. Strange it didn't maybe it is some kind of bug. Please file a bug report if you can. – Vivin Paliath Sep 01 '13 at 20:52
  • The time issue was between the JavaScript Date.getTime and the .NET method I described in my answer. Regula works beautifully. – sq33G Sep 02 '13 at 06:44
  • @sq33G Ah, ok! Good to hear! – Vivin Paliath Sep 02 '13 at 18:06
0

I found that the following hack works:

I added a second, hidden input that contains the date entered in the format of Date.getTime():

<input type="hidden" data-bind="value: myDateValue.getTime()" ... />

(I'm using Kendo MVVM, but I'm sure other MVVM libraries can handle the same approach)

Then for the constraint, on server side (ASP.NET MVC in my case) I'm generating the Unix time for the min and max:

@Range(min="<%= (dateTime1 - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds %>",
       max="<%= (dateTime2 - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds %>")

EDIT

Need to use .TotalMilliseconds - and even so there's some unexplained discrepancy.

sq33G
  • 3,320
  • 1
  • 23
  • 38