NotSooFriendly94

Plz help boss

Jan 17th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.36 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.   <style>
  6.     body {
  7.       display: flex;
  8.       justify-content: center;
  9.       align-items: center;
  10.       height: 100vh;
  11.     }
  12.  
  13.     .container {
  14.       text-align: center;
  15.       margin-bottom: 150px; /* Adjust the margin as needed */
  16.     }
  17.  
  18.     input[type="text"] {
  19.       font-size: 5vw;
  20.       padding: 10px;
  21.       margin-bottom: 10px;
  22.       text-align: center;
  23.     }
  24.  
  25.     button {
  26.       font-size: 5vw;
  27.       padding: 10px;
  28.       text-align: center;
  29.       position: relative; /* Position relative for child elements */
  30.       width: 150px; /* Set the desired width for the button */
  31.     }
  32.  
  33.     #loading-container {
  34.       position: relative;
  35.       margin-top: 40px; /* Set the margin beneath the button */
  36.     }
  37.  
  38.     #loading {
  39.       display: none;
  40.       position: absolute;
  41.       bottom: 0;
  42.       left: 0;
  43.       width: 0%;
  44.       height: 5px;
  45.       background-color: #4CAF50; /* Green loading bar color */
  46.       transition: width 5s; /* Transition duration */
  47.     }
  48.   </style>
  49. </head>
  50.  
  51. <body>
  52.   <div class="container">
  53.     <div>
  54.       <input type="text" id="postcodeInput" placeholder="Enter Post Code" onclick="this.placeholder=''"
  55.         style="font-size: 5vw;">
  56.       <br>
  57.       <button onclick="runScript()" style="font-size: 5vw;">Search</button>
  58.     </div>
  59.  
  60.     <div id="loading-container">
  61.       <div id="loading"></div>
  62.     </div>
  63.   </div>
  64.  
  65.   <script src="pcFinder.js"></script>
  66.   <script>
  67.     function runScript() {
  68.       console.log('Search button clicked');
  69.  
  70.       var inputValue = document.getElementById("postcodeInput").value;
  71.  
  72.       // Check if the input value is not empty
  73.       if (inputValue.trim() === "") {
  74.         console.log('Invalid input. Please enter a valid postcode.');
  75.         alert("Please enter a valid postcode.");
  76.         return;
  77.       }
  78.  
  79.       console.log('Calling getPC with input:', inputValue);
  80.  
  81.       getPC(inputValue)
  82.         .then(modifiedValue => {
  83.           console.log('getPC resolved. Modified Value:', modifiedValue);
  84.  
  85.           // Update input field with modified value
  86.           document.getElementById("postcodeInput").value = modifiedValue;
  87.  
  88.           // Show loading bar
  89.           var loadingBar = document.getElementById("loading");
  90.           loadingBar.style.display = "block";
  91.  
  92.           // Reset loading bar to 0% width
  93.           loadingBar.style.width = "0%";
  94.  
  95.           // Gradually fill the loading bar over 5 seconds
  96.           setTimeout(function () {
  97.             loadingBar.style.width = "100%";
  98.           }, 0); // Using a minimal delay for a smooth transition
  99.  
  100.           // Simulate processing logic (replace this with your actual logic)
  101.           setTimeout(function () {
  102.             // Hide loading bar
  103.             loadingBar.style.display = "none";
  104.             console.log('Processing completed');
  105.           }, 5000); // Replace 5000 with the actual processing time in milliseconds
  106.         })
  107.         .catch(error => {
  108.           console.error('Error:', error);
  109.           console.log('Invalid Postcode');
  110.           alert("Invalid Postcode");
  111.         });
  112.     }
  113.   </script>
  114. </body>
  115.  
  116. </html>
  117.  
  118.  
  119.  
  120. //Js file Code;
  121.  
  122.  
  123. function getPC(inputValue) {
  124.   var capitalizedValue = inputValue.toUpperCase();
  125.   var url = 'http://api.postcodes.io/postcodes/' + inputValue + '/validate';
  126.  
  127.   return fetch(url)
  128.     .then(response => {
  129.       if (!response.ok) {
  130.         throw new Error(`HTTP error! Status: ${response.status}`);
  131.       }
  132.       return response.text(); // Return the raw text for non-JSON responses
  133.     })
  134.     .then(data => {
  135.       try {
  136.         var jsonData = JSON.parse(data);
  137.         console.log('API Response:', jsonData);
  138.  
  139.         if (jsonData && jsonData.status === 200 && jsonData.result !== undefined) {
  140.           var modifiedValue = insertSpace(capitalizedValue, capitalizedValue.length - 3);
  141.           console.log('Modified Value:', modifiedValue);
  142.           return modifiedValue;
  143.         } else {
  144.           console.log('Invalid Postcode. Data:', jsonData);
  145.           return 'Invalid Postcode';
  146.         }
  147.       } catch (parseError) {
  148.         console.error('Error parsing JSON:', parseError);
  149.         return 'Invalid Postcode';
  150.       }
  151.     })
  152.     .catch(error => {
  153.       console.error('Error during fetch:', error.message || error);
  154.       throw error;  // Re-throw the error for further investigation
  155.     });
  156. }
  157.  
  158.  
  159.  
Advertisement
Add Comment
Please, Sign In to add comment