aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts
diff options
context:
space:
mode:
authorRobert2017-06-23 11:43:56 +0200
committerRobert2017-06-23 11:51:48 +0200
commit970954938043d8d73c4457ee2d91e22c0e422e65 (patch)
tree8c633f0437b0bd76c21d40d3bc7225230d8ce61f /app/assets/javascripts
parentd51985fc2a7c2138fd12cb9116ebf05d8b0e7dac (diff)
downloadchouette-core-970954938043d8d73c4457ee2d91e22c0e422e65.tar.bz2
JS for date validation
Diffstat (limited to 'app/assets/javascripts')
-rw-r--r--app/assets/javascripts/calendars.coffee35
1 files changed, 35 insertions, 0 deletions
diff --git a/app/assets/javascripts/calendars.coffee b/app/assets/javascripts/calendars.coffee
new file mode 100644
index 000000000..ac5330cc8
--- /dev/null
+++ b/app/assets/javascripts/calendars.coffee
@@ -0,0 +1,35 @@
+
+isEmpty = (x) -> x == ''
+
+daySelector = '#q_contains_date_3i'
+monthSelector = '#q_contains_date_2i'
+yearSelector = '#q_contains_date_1i'
+
+dateSelectors = [ daySelector, monthSelector, yearSelector ]
+
+checkDate = (args...) ->
+ vals = args.map((ele) -> ele.val())
+ return if vals.find( isEmpty ) == "" # no checking needed, no checking possible yet
+
+ dates = vals.map (x) -> parseInt(x)
+ return if isLegalDate(dates)
+ $('.alert').text('illegal date')
+ $('.alert').show()
+
+legalDaysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+
+isLeapYear = (year) ->
+ (year % 4 == 0) && ((year % 100 != 0) || (year % 1000 == 0))
+
+isLegalDate = (dates) ->
+ [day, month, year] = dates
+ return true if legalDaysPerMonth[month-1] >= day
+ return true if day == 29 && isLeapYear(year)
+ false
+
+defineChangeHandler = (selector) ->
+ $(selector).on 'change', ->
+ checkDate $(daySelector), $(monthSelector), $(yearSelector)
+
+$ ->
+ defineChangeHandler selector for selector in dateSelectors