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>
<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">
<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, ]*">
<button id="addbutton" class="btn btn-secondary" type="button">add</button>
</div>

View File

@ -53,15 +53,18 @@ render_graphs_wrapper();
document.getElementById("addbutton").addEventListener("click", 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 postData = {
itemid: document.getElementById("additemid").value,
attributes: document.getElementById("addattributes").value
itemid: inputFieldId.value,
attributes: inputFieldAttributes.value
};
// clear input fields
document.getElementById("additemid").value='';
document.getElementById("addattributes").value='';
inputFieldId.value='';
inputFieldAttributes.value='';
const requestOptions = {
method: 'POST',
@ -83,6 +86,9 @@ async function addItem() {
} else {
throw new Error('Error in server response');
}
} else {
console.log('invalid content in input fields')
}
}
async function toggle_hide(uuid) {