From 6d88929a695549f59811e4947f9c77aea4f37571 Mon Sep 17 00:00:00 2001 From: gaazzopa Date: Tue, 17 Nov 2020 19:10:13 +0100 Subject: [PATCH] Added non-zero validation --- cara/apps/calculator/static/js/form.js | 18 +++++++++++++++--- .../templates/calculator.form.html.j2 | 18 +++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cara/apps/calculator/static/js/form.js b/cara/apps/calculator/static/js/form.js index a4ea36d5..7195b2a2 100644 --- a/cara/apps/calculator/static/js/form.js +++ b/cara/apps/calculator/static/js/form.js @@ -234,9 +234,17 @@ function validate_form(form) { return submit; } -//function validateValue(obj) { -// $(obj).next('span').remove(); -//} +function validateNonZero(obj) { + $(obj).removeClass("red_border"); + $(obj).next('span').remove(); + + if ($(obj).val() == 0) { + $(obj).addClass("red_border"); + insertSpanAfter(obj, "Value must be > 0"); + return false; + } + return true; +} function validateDate(obj) { $(obj).removeClass("red_border"); @@ -314,6 +322,10 @@ $(document).ready(function () { $("#total_people").change(setMaxInfectedPeople); $("#activity_type").change(setMaxInfectedPeople); + //Validate values > 0 + $("input[required].non_zero").each(function() {validateNonZero(this)}); + $(".non_zero").change(function() {validateNonZero(this)}); + //Validate all dates $("input[required].datepicker").each(function() {validateDate(this)}); $(".datepicker").change(function() {validateDate(this)}); diff --git a/cara/apps/calculator/templates/calculator.form.html.j2 b/cara/apps/calculator/templates/calculator.form.html.j2 index 941903f1..bacb5ed3 100644 --- a/cara/apps/calculator/templates/calculator.form.html.j2 +++ b/cara/apps/calculator/templates/calculator.form.html.j2 @@ -42,10 +42,10 @@ ?
- Room volume:   
+ Room volume:   
- Floor area:   
-       Ceiling height:   
+ Floor area:   
+       Ceiling height:   

@@ -60,15 +60,15 @@