From 491e06bf7df4a2352cc6dd63e9b4651ea2035e31 Mon Sep 17 00:00:00 2001 From: Patrick Donelan Date: Mon, 24 Aug 2009 08:31:02 +0000 Subject: [PATCH] Fixed Suvery Number js validation Attaching the number validation code to onkeydown was very problematic. Changing to onblur is much cleaner, and probably nicer for the user. This commit also fixes the problem that Number questions without a 'step' set would not be checked for 'required'-ness. --- www/extras/wobject/Survey/administersurvey.js | 65 ++++++------------- 1 file changed, 19 insertions(+), 46 deletions(-) diff --git a/www/extras/wobject/Survey/administersurvey.js b/www/extras/wobject/Survey/administersurvey.js index 68d7e9fe6..a6134fb97 100644 --- a/www/extras/wobject/Survey/administersurvey.js +++ b/www/extras/wobject/Survey/administersurvey.js @@ -77,11 +77,11 @@ if (typeof Survey === "undefined") { answered = 1; for (var z1 in toValidate[i].answers) { if (YAHOO.lang.hasOwnProperty(toValidate[i].answers, z1)) { - var m = parseFloat(document.getElementById(z1).value); - var ansValues = toValidate[i].answers[z1]; - if((ansValues.max != '' && m > ansValues.max) || - (ansValues.min != '' && m < ansValues.min) || - (ansValues.step != '' && ( (m % ansValues.step) != 0) )){ + // We rely on server-side validation for actual value checking, + // and onblur js for user-friendly validation, so here we only + // need to check that required values look like numbers + var m = parseFloat(document.getElementById(z1).value, 10); + if (!YAHOO.lang.isNumber(m)) { answered = 0; break; } @@ -189,56 +189,28 @@ if (typeof Survey === "undefined") { function numberHandler(event, objs){ - var keycode = event.keyCode; var value = parseFloat(this.value, 10); - // Keep a record of the original value entered - var originalValue = value; - // Get the constraints var min = parseFloat(objs.min, 10); var max = parseFloat(objs.max, 10); var step = parseFloat(objs.step, 10); - // Response to up/down arrow keys - if (keycode == 38 || keycode == 40) { - // start at zero if we don't have a valid number - if (!YAHOO.lang.isNumber(value)) { - value = 0; + if (YAHOO.lang.isNumber(value)) { + // Enforce step + if (YAHOO.lang.isNumber(step) && value % step != 0){ + this.value = value - value % step; } - - // Default to 1 for increment steps - step = step ? step : 1; - - if (keycode == 38){ // up - value += step; - } - if (keycode == 40){ // down - value -= step; + // Enforce max/min constraints + if (YAHOO.lang.isNumber(min) && value < min){ + this.value = min; + } + if (YAHOO.lang.isNumber(max) && value > max){ + this.value = max; } } else { - // Apart from when we respond to up/down requests, if the input - // isn't a valid number it's best if we do nothing - the user - // might be half-way through entering something, and - // besides, validation will take care of it for us later - if (!YAHOO.lang.isNumber(value)) { - return; - } - } - - // Enforce max/min constraints - if (YAHOO.lang.isNumber(min) && value < min){ - value = min; - } - if (YAHOO.lang.isNumber(max) && value > max){ - value = max; - } - - // Only modify the value if the new numeric value - // is different from the original parsed value - // (so that "0.000" doesn't get turned into "0" etc..) - if (value != originalValue) { - this.value = value; + // Clear value if it is not a valid number + this.value = ''; } } @@ -752,7 +724,8 @@ if (typeof Survey === "undefined") { if (toValidate[q.id]) { toValidate[q.id].answers[q.answers[x1].id] = {'min':q.answers[x1].min,'max':q.answers[x1].max,'step':q.answers[x1].step}; } - YAHOO.util.Event.addListener(q.answers[x1].id, "keydown", numberHandler, q.answers[x1]); + // Attach to the onblur event rather than keydown otherwise things get very cludgy for the end-user + YAHOO.util.Event.addListener(q.answers[x1].id, "blur", numberHandler, q.answers[x1]); } } continue;