From 4ec08f11c65bd295f89d807575e2b21049d6ba7a Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Sat, 13 Jun 2020 11:57:37 +0200 Subject: [PATCH] Fix step validation of number field Current solution breaks if step is not an integer although HTML5 input fields of type number accept floating numbers. --- scripts/h5peditor-number.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/scripts/h5peditor-number.js b/scripts/h5peditor-number.js index 6663457c..32a5cf4e 100644 --- a/scripts/h5peditor-number.js +++ b/scripts/h5peditor-number.js @@ -92,6 +92,23 @@ ns.Number.prototype.createHtml = function () { ns.Number.prototype.validate = function () { var that = this; + /** + * Safely compute a remainder to avoid precision problems. + * @param {number} dividend Dividend. + * @param {number} divisor Divisor. + * @return {number} Remainder. + */ + const remainder = function (dividend, divisor) { + const decimalCount = Math.max( + (dividend.toString().split('.')[1] || '').length, + (divisor.toString().split('.')[1] || '').length + ); + return ( + parseInt(dividend.toFixed(decimalCount).replace('.','')) % + parseInt(divisor.toFixed(decimalCount).replace('.','')) + ) / Math.pow(10, decimalCount); + } + var value = H5P.trim(this.$input.val()); var decimals = this.field.decimals !== undefined && this.field.decimals; @@ -140,7 +157,7 @@ ns.Number.prototype.validate = function () { else if (this.field.min !== undefined && value < this.field.min) { this.$errors.append(ns.createError(ns.t('core', 'belowMin', {':property': propertyName, ':min': this.field.min}))); } - else if (this.field.step !== undefined && value % this.field.step) { + else if (this.field.step !== undefined && remainder(value, this.field.step)) { this.$errors.append(ns.createError(ns.t('core', 'outOfStep', {':property': propertyName, ':step': this.field.step}))); } }