WiP Validation stil.

This commit is contained in:
Naida Vatric
2020-02-12 23:43:59 +01:00
parent edb22266bd
commit 9c234a85fd
4 changed files with 142 additions and 49 deletions

View File

@@ -63,9 +63,7 @@ const BASIC_INPUT_PUBLISH = [
AD_CATEGORY.LAND, AD_CATEGORY.LAND,
AD_CATEGORY.GARAGE AD_CATEGORY.GARAGE
], ],
constraint: { constraint: ["numerical"]
numericality: true
}
}, },
{ {
dbField: "area", dbField: "area",
@@ -79,18 +77,13 @@ const BASIC_INPUT_PUBLISH = [
AD_CATEGORY.LAND, AD_CATEGORY.LAND,
AD_CATEGORY.GARAGE AD_CATEGORY.GARAGE
], ],
constraint: { constraint: ["numerical"]
numericality: true,
presence: true
}
}, },
{ {
dbField: "gardenSize", dbField: "gardenSize",
title: "Površina okućnice (m\xB2)", title: "Površina okućnice (m\xB2)",
categoriesToShow: [AD_CATEGORY.HOUSE, AD_CATEGORY.COTTAGE], categoriesToShow: [AD_CATEGORY.HOUSE, AD_CATEGORY.COTTAGE],
constraint: { constraint: ["numerical"]
numericality: true
}
}, },
{ {
dbField: "numberOfRooms", dbField: "numberOfRooms",
@@ -102,12 +95,7 @@ const BASIC_INPUT_PUBLISH = [
AD_CATEGORY.COTTAGE, AD_CATEGORY.COTTAGE,
AD_CATEGORY.OFFICE AD_CATEGORY.OFFICE
], ],
constraint: { constraint: ["integer"]
numericality: {
onlyInteger: true,
greaterThanOrEqualTo: 0
}
}
}, },
{ {
dbField: "numberOfFloors", dbField: "numberOfFloors",
@@ -118,11 +106,7 @@ const BASIC_INPUT_PUBLISH = [
AD_CATEGORY.APARTMENT, AD_CATEGORY.APARTMENT,
AD_CATEGORY.COTTAGE AD_CATEGORY.COTTAGE
], ],
constraint: { constraint: ["integer"]
numericality: {
onlyInteger: true
}
}
}, },
{ {
dbField: "floor", dbField: "floor",
@@ -132,11 +116,7 @@ const BASIC_INPUT_PUBLISH = [
AD_CATEGORY.APARTMENT, AD_CATEGORY.APARTMENT,
AD_CATEGORY.OFFICE AD_CATEGORY.OFFICE
], ],
constraint: { constraint: ["integer"]
numericality: {
onlyInteger: true
}
}
}, },
{ {
dbField: "title", dbField: "title",
@@ -150,9 +130,7 @@ const BASIC_INPUT_PUBLISH = [
AD_CATEGORY.LAND, AD_CATEGORY.LAND,
AD_CATEGORY.GARAGE AD_CATEGORY.GARAGE
], ],
constraint: { constraint: ["required"]
presence: true
}
}, },
{ {
dbField: "shortDescription", dbField: "shortDescription",
@@ -166,7 +144,7 @@ const BASIC_INPUT_PUBLISH = [
AD_CATEGORY.LAND, AD_CATEGORY.LAND,
AD_CATEGORY.GARAGE AD_CATEGORY.GARAGE
], ],
constraint: {} constraint: []
}, },
{ {
dbField: "streetName", dbField: "streetName",
@@ -180,7 +158,7 @@ const BASIC_INPUT_PUBLISH = [
AD_CATEGORY.LAND, AD_CATEGORY.LAND,
AD_CATEGORY.GARAGE AD_CATEGORY.GARAGE
], ],
constraint: {} constraint: []
} }
]; ];

View File

@@ -1,5 +1,5 @@
<br> <br>
<div class="row"> <div class="row" id="basic-inputs">
<% for (const input of basicInputInputs){ %> <% for (const input of basicInputInputs){ %>
<div class="input-field col s3 m4 l5"> <div class="input-field col s3 m4 l5">
<input <input

View File

@@ -32,10 +32,25 @@
$(document).ready(function(){ $(document).ready(function(){
$('.tabs').tabs(); $('.tabs').tabs();
const validateEmail = $email => { //Helper validation functions
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])?)*$/ const isValidEmail = $email => {
return regexEmail.test($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"); const form = document.querySelector("#publishForm");
@@ -64,6 +79,7 @@
$.each(errors, function(error) { $.each(errors, function(error) {
addError(messages, errors[error]); addError(messages, errors[error]);
}); });
} else { } else {
// otherwise we simply mark it as success // otherwise we simply mark it as success
formGroup.classList.add("has-success"); formGroup.classList.add("has-success");
@@ -100,6 +116,38 @@
messages.appendChild(block); 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 () { $("#submit").click( function () {
const mapBounds = map.getBounds(); const mapBounds = map.getBounds();
const currentLocation = marker.getPosition(); const currentLocation = marker.getPosition();
@@ -111,26 +159,43 @@
$("#west").val(mapBounds.getSouthWest().lng()); $("#west").val(mapBounds.getSouthWest().lng());
$("#lat").val(currentLocation.lat()); $("#lat").val(currentLocation.lat());
$("#lng").val(currentLocation.lng()); $("#lng").val(currentLocation.lng());
$("#locationInput").val( $("#locationInput").val(
document.getElementById("autocompleteInput").value 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)
}
}); });
}); });

50
help.js
View File

@@ -185,3 +185,53 @@
alert("Success!"); 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;
}
}