input fields validity check

This commit is contained in:
Sam Hadow 2024-02-17 00:18:24 +01:00
parent a2eea73a04
commit f1b698fb46
2 changed files with 31 additions and 25 deletions

View File

@ -22,7 +22,7 @@
<p></p> <p></p>
<div class="btn-toolbar btn-group-sm" role="toolbar" aria-label="Toolbar"> <div class="btn-toolbar btn-group-sm" role="toolbar" aria-label="Toolbar">
<div class="btn-group mr-2 w-50" role="group" aria-label="Add group"> <div class="btn-group mr-2 w-50" role="group" aria-label="Add group">
<input id="additemid" type="text" class="form-control input-sm w-15 w-50" placeholder="item id" pattern="[0-9]*"> <input id="additemid" type="text" class="form-control input-sm w-15 w-50" placeholder="item id" pattern="[0-9]+" required>
<input id="addattributes" type="text" class="form-control input-sm w-15" placeholder="attributes (comma separated)" pattern="[0-9a-zA-Z, ]*"> <input id="addattributes" type="text" class="form-control input-sm w-15" placeholder="attributes (comma separated)" pattern="[0-9a-zA-Z, ]*">
<button id="addbutton" class="btn btn-secondary" type="button">add</button> <button id="addbutton" class="btn btn-secondary" type="button">add</button>
</div> </div>

View File

@ -53,15 +53,18 @@ render_graphs_wrapper();
document.getElementById("addbutton").addEventListener("click", addItem); document.getElementById("addbutton").addEventListener("click", addItem);
async function addItem() { async function addItem() {
const inputFieldId = document.getElementById("additemid");
const inputFieldAttributes = document.getElementById("addattributes");
if (inputFieldId.checkValidity() && inputFieldAttributes.checkValidity()) {
const apiUrl = `${currentUrl}app/add`; const apiUrl = `${currentUrl}app/add`;
const postData = { const postData = {
itemid: document.getElementById("additemid").value, itemid: inputFieldId.value,
attributes: document.getElementById("addattributes").value attributes: inputFieldAttributes.value
}; };
// clear input fields // clear input fields
document.getElementById("additemid").value=''; inputFieldId.value='';
document.getElementById("addattributes").value=''; inputFieldAttributes.value='';
const requestOptions = { const requestOptions = {
method: 'POST', method: 'POST',
@ -83,6 +86,9 @@ async function addItem() {
} else { } else {
throw new Error('Error in server response'); throw new Error('Error in server response');
} }
} else {
console.log('invalid content in input fields')
}
} }
async function toggle_hide(uuid) { async function toggle_hide(uuid) {