WiP Validation stil.
This commit is contained in:
50
help.js
50
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user