diff --git a/static/scripts/frontpage3d.js b/static/scripts/frontpage3d.js index 7491e3e8..1a625682 100644 --- a/static/scripts/frontpage3d.js +++ b/static/scripts/frontpage3d.js @@ -807,3 +807,24 @@ function toggle3DPO(){ } } +function onFooterSubmit(){ + var form = $("#settingsForm") + // JQuery returns list of forms so must select first [0]. + isValid = form[0].checkValidity(); + if (isValid) { + var url = $("#pageID").val() + $.ajax({ + url : '/'+url, + type: "POST", + data: form.serialize(), + success: function (data) { + console.log("success"); + $('#contentModal').modal('toggle') + }, + error: function (jXHR, textStatus, errorThrown) { + alert(errorThrown); + } + }); + } +} + diff --git a/static/scripts/frontpage3dcontrols.js b/static/scripts/frontpage3dcontrols.js index d55cf71b..fb2560f8 100644 --- a/static/scripts/frontpage3dcontrols.js +++ b/static/scripts/frontpage3dcontrols.js @@ -152,3 +152,11 @@ function processStatusMessage(data){ else $("#currentPositioningMode").text("Incremental (G91)"); } + +function inputDecimalValidate() { + var re = new RegExp($(this).attr("pattern")); + valid = pattern.test($(this).val()); + if (!valid) { + $(this).setCustomValidity("Decimal input pattern mismatch"); + } +} diff --git a/static/scripts/frontpageControls.js b/static/scripts/frontpageControls.js index dc0bb784..42aeb37f 100644 --- a/static/scripts/frontpageControls.js +++ b/static/scripts/frontpageControls.js @@ -22,6 +22,25 @@ $(document).ready(function(){ var controllerMessage = document.getElementById('controllerMessage'); controllerMessage.scrollTop = controllerMessage.scrollHeight; $("#stickyButtons").css("top", $(".navbar").outerHeight()); + + // Set custom validation on all form inputs of type 'number' with attribute 'pattern'. + // Input type 'number' does not support 'pattern' attribute by default using HTML5. + // Input type 'number' always returns a value compatible with parseFloat using period as decimal separator. + // @todo: This event handler may need to be moved to other JS files for more/less global application. + $(':input[type="number"][pattern]').change(function () { + var re = new RegExp($(this).attr("pattern")); + var isValid = re.test($(this).val()); + // JQuery selector returns array of possible matches. + // Validity checks must be applied to first item [0]. + if (isValid) { + // Set empty value to clear error. + $(this)[0].setCustomValidity(""); + } + else { + // Set title as error message. + $(this)[0].setCustomValidity($(this).attr("title")); + } + }); }); function pauseRun(){ @@ -138,13 +157,12 @@ function boardDataUpdate(data){ } function moveAction(direction) { - distance = $("#distToMove").val(); - distanceValid = distance.search(/^[0-9]*(\.[0-9]{0,3})?$/); - if (distanceValid == 0) { - action('move', direction, distance); - } else { - $("#distToMove").focus(); - } + // JQuery returns list of matching forms so select first [0] for validitity checking. + var isValid = $("#distInput")[0].checkValidity(); + if (isValid) { + distance = $("#distToMove").val(); + action('move', direction, distance); + } } function processStatusMessage(data){ diff --git a/static/scripts/settings.js b/static/scripts/settings.js index ea7a5472..45271612 100644 --- a/static/scripts/settings.js +++ b/static/scripts/settings.js @@ -1,9 +1,11 @@ function onFooterSubmit(){ + var form = $("#settingsForm") + if (form[0].checkValidity()) { var url = $("#pageID").val() $.ajax({ url : '/'+url, type: "POST", - data: $("#settingsForm").serialize(), + data: form.serialize(), success: function (data) { console.log("success"); $('#contentModal').modal('toggle') @@ -12,8 +14,10 @@ function onFooterSubmit(){ alert(errorThrown); } }); + } } +/* $(document).ready(function () { $('#settingsForm').on('submit', function(e) { e.preventDefault(); @@ -38,6 +42,7 @@ $(document).ready(function () { }); }); +*/ function updatePorts(data){ var selectedPort = $("#comPorts").find(":selected").text(); diff --git a/templates/editBoard.html b/templates/editBoard.html index d0faf3c2..b0ab1c09 100644 --- a/templates/editBoard.html +++ b/templates/editBoard.html @@ -1,4 +1,4 @@ - +{% import 'macros.html' as macros %} {% block content %}