NotSooFriendly94

Pc

Jan 18th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 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. async function runScript() {
  68. console.log('Search button clicked');
  69.  
  70. var inputValue = document.getElementById("postcodeInput").value;
  71. console.log('Input value:', inputValue);
  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 Validate with input:', inputValue);
  80.  
  81. // Show loading bar
  82. var loadingBar = document.getElementById("loading");
  83. loadingBar.style.display = "block";
  84. loadingBar.style.width = "0%";
  85.  
  86. try {
  87. isValid = await validate(inputValue);
  88.  
  89. if (isValid) {
  90. try {
  91. const modifiedValue = await getPC(inputValue);
  92.  
  93. console.log('getPC resolved. Modified Value:', modifiedValue);
  94.  
  95. // Update input field with modified value
  96. document.getElementById("postcodeInput").value = modifiedValue;
  97.  
  98. // Gradually fill the loading bar over 5 seconds
  99. setTimeout(function () {
  100. loadingBar.style.width = "100%";
  101. }, 0); // Using a minimal delay for a smooth transition
  102.  
  103. // Simulate processing logic (replace this with your actual logic)
  104. setTimeout(function () {
  105. // Hide loading bar
  106. loadingBar.style.display = "none";
  107. console.log('Processing completed');
  108. }, 5000); // Replace 5000 with the actual processing time in milliseconds
  109. } catch (error) {
  110. console.error('Error:', error);
  111. console.log('Invalid Postcode');
  112. alert("Invalid Postcode");
  113. // Set the input box text to "Invalid postcode"
  114. document.getElementById("postcodeInput").value = "Invalid postcode";
  115. }
  116. } else {
  117. // Handle case where validation is false
  118. console.log('Invalid Postcode');
  119. alert("Invalid Postcode");
  120. // Set the input box text to "Invalid postcode"
  121. document.getElementById("postcodeInput").value = "Invalid postcode";
  122. // Hide loading bar
  123. loadingBar.style.display = "none";
  124. }
  125. } catch (error) {
  126. console.error('Error:', error);
  127. }
  128. }
  129. </script>
  130. </body>
  131.  
  132. </html>
  133.  
  134.  
  135. PcFinder.js
  136.  
  137. function getPC(inputValue) {
  138. var uppercasedValue = inputValue.toUpperCase();
  139. var modifiedValue = uppercasedValue.slice(0, -3) + ' ' + uppercasedValue.slice(-3);
  140.  
  141. return modifiedValue;
  142. }
  143.  
  144. function validate(inputValue) {
  145. // Remove spaces from the inputValue
  146. var inputValueStr = inputValue.replace(/\s/g, '');
  147.  
  148. var url = 'https://api.postcodes.io/postcodes/' + inputValueStr + '/validate';
  149. console.log(url);
  150.  
  151. var options = {
  152. method: 'GET',
  153. headers: {
  154. 'Content-Type': 'application/json'
  155. }
  156. };
  157.  
  158. console.log('before fetch');
  159.  
  160. return fetch(url, options)
  161. .then((response) => response.json())
  162. .then((data) => {
  163. var validation = data.result;
  164. console.log(validation);
  165. return validation; // return the validation result if needed
  166. });
  167. }
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment