From 21436340cd2bcb5cf7fdc1c4c71bd3e2c04cb7e9 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Fri, 23 Mar 2018 12:25:50 +0000 Subject: [PATCH] Add more client-side field validation --- .../dialogs/schema-alignment-dialog.js | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js b/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js index 8610b7f2d..f508efb6d 100644 --- a/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js +++ b/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js @@ -716,8 +716,9 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue, changedCallback(); }); + SchemaAlignmentDialog.setupStringInputValidation(input, /^\d{4}(-[0-1]\d(-[0-3]\d(T[0-2]\d(:[0-5]\d(:[0-5]\dZ)?)?)?)?)?$/); } else if (mode === "globe-coordinate") { - input.attr("placeholder", "lat/lon"); + input.attr("placeholder", "lat,lon"); var propagateValue = function(val) { // TODO add validation here inputContainer.data("jsonValue", { @@ -731,6 +732,7 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue, changedCallback(); }); + SchemaAlignmentDialog.setupStringInputValidation(input, /^[\-+]?\d+(\.\d*)?[,\/][\-+]?\d+(\.\d*)?([,\/]\d+(\.\d*)?)?$/); } else if (mode === "language") { input.attr("placeholder", "lang"); input.addClass("wbs-language-input"); @@ -818,16 +820,22 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue, }); if (mode === "amount") { input.attr("placeholder", $.i18n._('wikidata-schema')["amount"]); + SchemaAlignmentDialog.setupStringInputValidation(input, /^[\-+]?\d+(\.\d*)?(E[\-+]\d+)?$/); } else if (mode === "url") { input.attr("placeholder", $.i18n._('wikidata-schema')["full-url"]); + SchemaAlignmentDialog.setupStringInputValidation(input, /^https?:\/\/.+$/); } else if (mode === "tabular-data") { input.attr("placeholder", $.i18n._('wikidata-schema')["tabular-data-with-prefix"]); + SchemaAlignmentDialog.setupStringInputValidation(input, /^Data:.+$/); } else if (mode === "commonsMedia") { input.attr("placeholder", $.i18n._('wikidata-schema')["commons-media"]); } else if (mode === "math") { input.attr("placeholder", $.i18n._('wikidata-schema')["math-expression"]); } else if (mode === "geo-shape") { input.attr("placeholder", $.i18n._('wikidata-schema')["geoshape-with-prefix"]); + SchemaAlignmentDialog.setupStringInputValidation(input, /^Data:.+$/); + } else { + SchemaAlignmentDialog.setupStringInputValidation(input, /^.+$/); } if (mode !== "external-id" && mode !== "url" && @@ -919,6 +927,19 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue, } } +SchemaAlignmentDialog.setupStringInputValidation = function(input, regex) { + input.focus(function() { + input.removeClass('wbs-unvalidated-input'); + }).blur(function() { + var currentValue = input.val(); + if (regex.test(currentValue)) { + input.addClass('wbs-validated-input'); + } else { + input.addClass('wbs-unvalidated-input'); + } + }); +} + SchemaAlignmentDialog._inputContainerToJSON = function (inputContainer) { var data = inputContainer.data(); if (data) {