Advertisement
plirof2

generate numbers with pre/post text v05c [text1] ,SORT

Mar 14th, 2024 (edited)
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <!--
  5. changes:
  6. v240316b v05c Added [text1] and [text2] and SORT tags to replace with static text
  7. v240314b v05b
  8.     -->
  9.     <title>Number Sequence Generator</title>
  10. </head>
  11. <body>
  12.     <label for="sentence">Enter sentence:</label>
  13.     <input type="text" id="sentence" name="sentence" value="test[x]bbbb[entry]vvvv[x]"><br><br>
  14.    
  15.     <label for="num_lower">Lower Number:</label>
  16.     <input type="number" id="num_lower" name="num_lower" value=1 ><br><br>
  17.  
  18.     <label for="num_higher">Higher Number:</label>
  19.     <input type="number" id="num_higher" name="num_higher" value=15 ><br><br>
  20.  
  21.     <label for="textadd1">Enter text1 for replacing a [text1] tag :</label>
  22.     <input type="text" id="textadd1" name="textadd1"><br><br> <!-- Added text input for replacement text -->
  23.  
  24.     <label for="textadd2">Enter text1 for replacing a [text2] tag :</label>
  25.     <input type="text" id="textadd2" name="textadd2"><br><br> <!-- Added text input for replacement text -->
  26.  
  27.  
  28.     <label for="zero_padding">Zero Padding:</label>
  29.     <input type="checkbox" id="zero_padding" name="zero_padding"><br><br>
  30.  
  31.     <label for="sort_result">Sort Result:</label>
  32.     <input type="checkbox" id="sort_result" name="sort_result" checked ><br><br>
  33.  
  34.     <button onclick="generateSequence()">Generate Sequence</button><br><br>
  35.  
  36.     <textarea id="result" rows="10" cols="50"></textarea>
  37.  
  38.     <script>
  39.         function generateSequence() {
  40.             var sentence = document.getElementById('sentence').value;
  41.             var num_lower = parseInt(document.getElementById('num_lower').value);
  42.             var num_higher = parseInt(document.getElementById('num_higher').value);
  43.             var textadd1 = document.getElementById('textadd1').value; // Get the text for replacement
  44.             var textadd2 = document.getElementById('textadd2').value; // Get the text for replacement
  45.             var zero_padding = document.getElementById('zero_padding').checked;
  46.             var sort_result = document.getElementById('sort_result').checked; // Check if sort result is checked
  47.             var result = '';
  48.             //let lineIndex = 0;
  49.  
  50.             for (var i = num_lower; i <= num_higher; i++) {
  51.                 (function(i) {
  52.                     var num = zero_padding ? String(i).padStart(String(num_higher).length, '0') : i;
  53.                     var entry = sentence.replace(/\[x\]/g, num);
  54.                     entry = entry.replace(/\[text1\]/g, textadd1); // Replace [text] with the contents of textadd1
  55.                     entry = entry.replace(/\[text2\]/g, textadd2); // Replace [text] with the contents of textadd1
  56.                     //console.log("entry=" + entry + "   num=" + num);
  57.  
  58.                     if (entry.includes('[entry]')) {
  59.                         fetch('entries.txt').then(response => response.text()).then(text => {
  60.                             var lines = text.split('\n');
  61.                             //console.log("entry222222222=" + entry + "   num=" + num + "   i=" + i);
  62.                             var entryWithEntries = entry.replace(/\[entry\]/g, lines[i-1] || '');
  63.                             //console.log(lines);
  64.                             //console.log("entry3333333=" + entry + "   num=" + num + "   i=" + i);
  65.                             result += entryWithEntries + '\n';
  66.                             document.getElementById('result').value = result;
  67.                         });
  68.                     } else {
  69.                         result += entry + '\n';
  70.                         document.getElementById('result').value = result;
  71.                     }
  72.                 })(i);
  73.             }
  74.             if(sort_result) {
  75.                 //console.log("sort_result="+sort_result);
  76.                 setTimeout(() => {
  77.                     // Your code after pausing for 1 second
  78.                     var textArea = document.getElementById('result');
  79.                     var text = textArea.value;
  80.                     var sortedText = text.split('\n').filter(line => line.trim() !== '').sort().join('\n'); // Sort the text lines alphabetically & // Remove empty lines
  81.                     textArea.value = sortedText; // Update the textarea with the sorted text
  82.                 }, 1000); // 1000 milliseconds = 1 second
  83.             }// end of if(sortResult) {
  84.         } //end of function generateSequence()
  85.     </script>
  86. </body>
  87. </html>
  88. <pre>
  89. ```
  90. Hello[x]σδδασδ[x]d[entry]sada
  91.  
  92. Make sure to create a file named `entries.txt` containing the strings you want to replace `[entry]` in the sentence with.
  93.  
  94.  
  95. I want a single html page with included javascript that will have a text input named "sentence".
  96. Then it will have two more text inputs named num_lower and num_higher. At the bottom will have a submit button and a text area for the output/result .  When I press the submit button I want the script to generate a sequence of numbers between num_lower and num_higher. Then I want in the result textarea to show one entry for each number. Each entry will be the text entered in the "sentence" text input where we will replace all instances of string "[x]" with the current generated number.
  97. I also want a check box to select if we want the numbers to be prefixed with zeroes so they are always the same character size as the number in input "number_higher" (eg 001,002,...,100). I also want another feature: if in the sentence text we have the string [entry] then the script wil read the file "entries.txt". This file will contain a string in each line. I want each [entry] to be replaced by the string contained in the current line of the file "entries.txt" , then we move to the next line of entries.txt
  98.  
  99.  
  100. sample entries.txt
  101. aaaaa
  102. bbbbb
  103. ccccc
  104. ddddd
  105. eeeee
  106.  
  107. </pre>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement