From 9c234a85fd04daba73cbca843f993f320a33f8cf Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Wed, 12 Feb 2020 23:43:59 +0100 Subject: [PATCH] WiP Validation stil. --- app/common/publishEnums.js | 40 +++---------- app/views/publishBasicData.ejs | 2 +- app/views/publishRealEstate.ejs | 99 +++++++++++++++++++++++++++------ help.js | 50 +++++++++++++++++ 4 files changed, 142 insertions(+), 49 deletions(-) diff --git a/app/common/publishEnums.js b/app/common/publishEnums.js index d034faf..c78ebce 100644 --- a/app/common/publishEnums.js +++ b/app/common/publishEnums.js @@ -63,9 +63,7 @@ const BASIC_INPUT_PUBLISH = [ AD_CATEGORY.LAND, AD_CATEGORY.GARAGE ], - constraint: { - numericality: true - } + constraint: ["numerical"] }, { dbField: "area", @@ -79,18 +77,13 @@ const BASIC_INPUT_PUBLISH = [ AD_CATEGORY.LAND, AD_CATEGORY.GARAGE ], - constraint: { - numericality: true, - presence: true - } + constraint: ["numerical"] }, { dbField: "gardenSize", title: "Površina okućnice (m\xB2)", categoriesToShow: [AD_CATEGORY.HOUSE, AD_CATEGORY.COTTAGE], - constraint: { - numericality: true - } + constraint: ["numerical"] }, { dbField: "numberOfRooms", @@ -102,12 +95,7 @@ const BASIC_INPUT_PUBLISH = [ AD_CATEGORY.COTTAGE, AD_CATEGORY.OFFICE ], - constraint: { - numericality: { - onlyInteger: true, - greaterThanOrEqualTo: 0 - } - } + constraint: ["integer"] }, { dbField: "numberOfFloors", @@ -118,11 +106,7 @@ const BASIC_INPUT_PUBLISH = [ AD_CATEGORY.APARTMENT, AD_CATEGORY.COTTAGE ], - constraint: { - numericality: { - onlyInteger: true - } - } + constraint: ["integer"] }, { dbField: "floor", @@ -132,11 +116,7 @@ const BASIC_INPUT_PUBLISH = [ AD_CATEGORY.APARTMENT, AD_CATEGORY.OFFICE ], - constraint: { - numericality: { - onlyInteger: true - } - } + constraint: ["integer"] }, { dbField: "title", @@ -150,9 +130,7 @@ const BASIC_INPUT_PUBLISH = [ AD_CATEGORY.LAND, AD_CATEGORY.GARAGE ], - constraint: { - presence: true - } + constraint: ["required"] }, { dbField: "shortDescription", @@ -166,7 +144,7 @@ const BASIC_INPUT_PUBLISH = [ AD_CATEGORY.LAND, AD_CATEGORY.GARAGE ], - constraint: {} + constraint: [] }, { dbField: "streetName", @@ -180,7 +158,7 @@ const BASIC_INPUT_PUBLISH = [ AD_CATEGORY.LAND, AD_CATEGORY.GARAGE ], - constraint: {} + constraint: [] } ]; diff --git a/app/views/publishBasicData.ejs b/app/views/publishBasicData.ejs index e1dd8fb..322eeec 100644 --- a/app/views/publishBasicData.ejs +++ b/app/views/publishBasicData.ejs @@ -1,5 +1,5 @@
-
+
<% for (const input of basicInputInputs){ %>
{ - const regexEmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ - return regexEmail.test($email); - } + //Helper validation functions + const isValidEmail = $email => { + const simpleEmailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + return $email && $email.length < 250 && simpleEmailRegex.test($email); + }; + + const isPresent = $input => { + return $input && $input!=="" && $input != null; + }; + + const isNumber = $input => { + const simpleNumberRegex = /([0-9]+[.|,][0-9])|([0-9][.|,][0-9]+)|([0-9]+)/; + return $input && $input.length <250 && simpleNumberRegex.test($input) && !isNaN($input); + }; + + const isInteger = $input => { + const simpleIntegerRegex = /^([+-]?[1-9]\d*|0)$/; + return $input && $input.length <250 && simpleIntegerRegex.test($input); + }; const form = document.querySelector("#publishForm"); @@ -64,6 +79,7 @@ $.each(errors, function(error) { addError(messages, errors[error]); }); + } else { // otherwise we simply mark it as success formGroup.classList.add("has-success"); @@ -100,6 +116,38 @@ messages.appendChild(block); } + const validate = (input) => { + + let valid=true; + let errorMsg =[]; + let constraint = input.constraint[0]; + + switch (constraint) { + case "required": + valid = isPresent ($(`#${input.dbField}`).val()); + errorMsg = ["Ovo je obavezno polje."]; + break; + case "numerical": + valid = isNumber ($(`#${input.dbField}`).val()); + errorMsg = ["Unesite brojcanu vrijednost."]; + break; + case "integer": + valid = isInteger ($(`#${input.dbField}`).val()); + errorMsg = ["Unesite cijeli broj."]; + break; + default : + valid = true; + } + if (!valid) { + const inputField = document.querySelector(`#${input.dbField}`); + showErrorsForInput( inputField, errorMsg); + return false; + } else { + return true; + } + +} + $("#submit").click( function () { const mapBounds = map.getBounds(); const currentLocation = marker.getPosition(); @@ -111,26 +159,43 @@ $("#west").val(mapBounds.getSouthWest().lng()); $("#lat").val(currentLocation.lat()); + $("#lng").val(currentLocation.lng()); $("#locationInput").val( document.getElementById("autocompleteInput").value ); + //Tag for checking of error presence + let hasErrors = false; + //Check if email is valid + const validEmail = isValidEmail($("#email").val()); + //Show messeges for invalid email is present + if (!validEmail) { + const errorMsgs = ["Unesite validan email."]; + const email = document.querySelector("#email"); + showErrorsForInput( email, errorMsgs) + hasErrors = true; + }; + //Check if other input fields are valid - vratiti se na ovo!! + //const basicInputInputs= document.getElementById("basic-inputs").getElementsByTagName("input"); + + //alert(JSON.stringify("")); + /* + $.each(basicInputInputs, function (input) { + alert(input); + validate (input); + }) + for (const input of basicInputInputs ) { + alert(input.getAttribute(name)); + + validate (input); + } */ + + if (!hasErrors) { + $("#publishForm").submit(); + }; - const errors = validateEmail($("#email").val()); - - if (errors) { - $("#publishForm").submit(); - } - else { - const errorMsgs = ["Unesite validan email."]; - const email = document.querySelector("#email"); - - showErrorsForInput( email, errorMsgs) - - - } }); }); diff --git a/help.js b/help.js index 58df1bf..cc0b55c 100644 --- a/help.js +++ b/help.js @@ -185,3 +185,53 @@ alert("Success!"); } })(); +///////////////////////////////////////////////// +const isPresent = $input => { + return $input && $input!=="" && $input != null; +} + +const isNumber = $input => { + const simpleNumberRegex = /[+-]?(?:\d*[.,])?\d+/; + return $input && $input.length <250 && simpleNumberRegex.test($input); + +} + +const isInteger = $input => { + const simpleIntegerRegex = /^([+-]?[1-9]\d*|0)$/; + return $input && $input.length <250 && simpleIntegerRegex.test($input); + +} + +const validate = (input) => { + + const valid; + const errorMsg; + const constraint = input.constraint[0]; + + switch (constraint) { + case "required": + valid = isPresent ($(`#${input.dbField}`).val()); + errorMsg = ["Ovo je obavezno polje."]; + break; + case "numerical": + valid = isNumber ($(`#${input.dbField}`).val()); + errorMsg = ["Unesite brojcanu vrijednost."]; + break; + case "integer": + valid = isInteger ($(`#${input.dbField}`).val()); + errorMsg = ["Unesite cijeli broj."]; + + break; + default : + valid = true; + } + if (!valid) { + const inputField = document.querySelector(`#${input.dbField}`); + showErrorsForInput( inputField, errorMsg); + return false; + } else { + return true; + } + + } +