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.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: []
}
];

View File

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

View File

@@ -32,10 +32,25 @@
$(document).ready(function(){
$('.tabs').tabs();
const validateEmail = $email => {
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)
}
});
});

50
help.js
View File

@@ -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;
}
}