From b2266ada26ebbcb4597f631c96d7885f62257159 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 26 Sep 2023 12:48:14 +0800 Subject: [PATCH] globalstock validation --- static/app.js | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/static/app.js b/static/app.js index 1eb4fd2..cea7a56 100755 --- a/static/app.js +++ b/static/app.js @@ -2594,6 +2594,126 @@ var specialChars = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,<>\/?~]/; } + +// global Stocks validation + +function globalstockValidate() { + + let x = document.getElementById("globalstocks_symbol").value; + let portfolioCheckbox = document.getElementById("globalstocks_portfolio"); + let sharesText = document.getElementById("globalstocks_port_shares").value; + let averageCost = document.getElementById("globalstocks_port_cost").value; + let dateText = document.getElementById("globalstocks_port_date").value; + + let text; + let text2; + if (x.toUpperCase() != x) { + document.getElementById('demo10').style.display = "block"; + text = "Stock symbol must be uppercase"; + document.getElementById("globalstocks_symbol").value = ''; + // createLi = false; + } + else if (x === '') { + document.getElementById('demo10').style.display = "block"; + text = "No blanks" + // createLi = false; + } + else if (x.includes(' ')) { + text = "No spaces"; + document.getElementById('demo10').style.display = "block"; + document.getElementById("globalstocks_symbol").value = ''; + // createLi = false; + } + else { + text = "Input OK"; + document.getElementById('demo10').style.display = "none"; + //PORTFOLIO VALIDATION HERE + if (portfolioCheckbox.checked) { + if ((sharesText === ' ') || (averageCost === ' ') || (dateText === ' ')) { + text2 = "No spaces"; + document.getElementById('demo-portfolio-globalstocks').style.display = "block"; + document.getElementById("globalstocks_port_shares").value = ''; + document.getElementById("globalstocks_port_cost").value = ''; + document.getElementById("globalstocks_port_date").value = ''; + document.getElementById("globalstocks_symbol").value = ''; + } + else if ((isNaN(sharesText))|| (isNaN(averageCost))) { + text2 = "No text characters, only numbers"; + document.getElementById('demo-portfolio-globalstocks').style.display = "block"; + document.getElementById("globalstocks_port_shares").value = ''; + document.getElementById("globalstocks_port_cost").value = ''; + document.getElementById("globalstocks_port_date").value = ''; + document.getElementById("globalstocks_symbol").value = ''; + } + else if ((!dateText.includes('-')) && (dateText !== '')) { + text2 = "Incorrect date format, it should be YYYY-MM-DD"; + document.getElementById('demo-portfolio-globalstocks').style.display = "block"; + document.getElementById("globalstocks_port_shares").value = ''; + document.getElementById("globalstocks_port_cost").value = ''; + document.getElementById("globalstocks_port_date").value = ''; + document.getElementById("globalstocks_symbol").value = ''; + } + else if (((sharesText !== '') || (averageCost !== '') || (dateText !== '')) && ((sharesText === '') || (averageCost === '') || (dateText === ''))) { + text2 = "Some fields are empty"; + document.getElementById('demo-portfolio-globalstocks').style.display = "block"; + document.getElementById("globalstocks_port_shares").value = ''; + document.getElementById("globalstocks_port_cost").value = ''; + document.getElementById("globalstocks_port_date").value = ''; + document.getElementById("globalstocks_symbol").value = ''; + } + else { + text2 = "Input OK"; + document.getElementById('demo-portfolio-globalstocks').style.display = "none"; + createLi = true; + // CALL ADDING ITEMS FUNCTION SO THAT ONLY VALIDATED ITEMS CAN GET ADDED TO LIST + // addingItems(); + let cost = document.getElementById('globalstocks_port_cost').value; + let shares = document.getElementById('globalstocks_port_shares').value; + let symbol = document.getElementById('globalstocks_symbol').value; + let days = document.getElementById('globalstocks_port_date').value; + + let settings = { + shares:shares, + cost:cost, + symbol:symbol, + days:days, + }; + console.log(JSON.stringify(settings)); + + setTimeout( + function() { + document.getElementById('globalstocks_symbol').value=""; + document.getElementById("globalstocks_port_shares").value = ''; + document.getElementById("globalstocks_port_cost").value = ''; + document.getElementById("globalstocks_port_date").value = ''; + document.getElementById("globalstocks_symbol").value = ''; + }, 100); + + if ((sharesText !== '') && (averageCost !== '') && (dateText !== '')) { + fetch("/saveGlobalPortfolioSettings", { + method: "POST", + body: JSON.stringify(settings), + }); + } + } + document.getElementById("demo-portfolio-globalstocks").innerHTML = text2; + } + + else { + createLi = true; + // CALL ADDING ITEMS FUNCTION SO THAT ONLY VALIDATED ITEMS CAN GET ADDED TO LIST + // addingItems(); + setTimeout( + function() { + document.getElementById('globalstocks_symbol').value=""; + }, 100); + } + } + document.getElementById("demo10").innerHTML = text; + } + + + // Crypto validation function cryptoValidate() {