Mcluffin

form_practice

Oct 1st, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 6.89 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <!-- Name: Tylor McLaughlin
  4.      Description: This html page was created as part of an exercise to get more familiar with Forms and page layouts
  5.      Date: September 30, 2015
  6. -->
  7.     <head>
  8.         <title>SET Registration</title>
  9.         <style>        
  10.             html, body, form {
  11.                 height: 100%;
  12.             }
  13.            
  14.             td.odd {
  15.                 background-color: gray;
  16.             }
  17.         </style>
  18.        
  19.         <script>
  20.             //This function creates the appropriate set of checkboxes for the user to see based on what radio
  21.             //button is currently selected.
  22.             function displayOptions()
  23.             {
  24.                 var options = document.getElementsByName("occupation");
  25.                
  26.                 for(var elem in options)
  27.                 {
  28.                     if(options[elem].checked)
  29.                     {
  30.                         if (options[elem].value == "student")
  31.                         {
  32.                             document.getElementById("details_txt").innerHTML = '' +
  33.                                 "<input name='it_check' type='checkbox' value='it'>Student in Information Technology<br>" +
  34.                                 "<input name='laptop_check' type='checkbox' value='laptop'>Owns a laptop<br>" +
  35.                                 "<input name='bus_check' type='checkbox' value='bus'>Takes bus to school<br>" +
  36.                                 "<input name='music_check' type='checkbox' value='music'>Owns a portable music player";
  37.                         }
  38.                         else if (options[elem].value == "faculty")
  39.                         {
  40.                             document.getElementById("details_txt").innerHTML = '' +
  41.                             '<input name="eng_check" type="checkbox" value="eng">Teaches in Engineering<br>' +
  42.                             '<input name="car_check" type="checkbox" value="car">Drives car to work<br>' +
  43.                             '<input name="lunch_check" type="checkbox" value="lunch">Brings lunch<br>';
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.            
  49.             function resetForm()
  50.             {
  51.                 document.getElementById("deets").reset();
  52.                 document.getElementById("name_err").innerHTML = "";
  53.                 document.getElementById("street_err").innerHTML = "";
  54.                 document.getElementById("city_err").innerHTML = "";
  55.                 document.getElementById("prov_err").innerHTML = "";
  56.                 document.getElementById("postal_err").innerHTML = "";
  57.                 document.getElementById("type_err").innerHTML = "";
  58.                 document.getElementById("details_err").innerHTML = "";
  59.                 displayOptions();
  60.             }
  61.            
  62.             //This method is called before the form is submitted to validate the format of the postal code
  63.             function checkForm()
  64.             {
  65.                 //According to https://regex101.com/ this regex works as intended
  66.                 var postalcode_check = new RegExp(/^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]( )?\d[ABCEGHJKLMNPRSTVWXYZ]\d$/i);
  67.                
  68.                 //This seems to be getting skipped, so as long as the postal code field is not left empty, the form
  69.                 //will be submitted. Really have no idea why. It did work at one point. Tried a lot of things to get
  70.                 //the "required" attribute to work with a Submit button because form.submit doesn't actually check for the
  71.                 //"required" attribute. I don't know why the form gets submitted if the postal code doesn't fit the regex.
  72.                 if (postalcode_check.test(document.getElementByName("postalcode_box").value))
  73.                 {
  74.                     return true;
  75.                 }
  76.                 else
  77.                 {
  78.                     document.getElementById("postal_err").innerHTML = "Please enter a valid postal code.";
  79.                     return false;
  80.                 }
  81.                
  82.                 return false;
  83.             }
  84.         </script>
  85.     </head>
  86.     <body>
  87.         <form id="deets" action="testRequest.asp" onsubmit="return checkForm()" method="GET">
  88.             <div style="height:20%; width:100%;"></div>
  89.             <!-- Turns out any input that isn't given a name attribute won't get passed on submit... -->
  90.             <table id="content" align="center" style="table-layout: fixed; width: 100%">
  91.                 <tr>
  92.                     <td id="name_err" width="15%"></td>
  93.                     <td id="name_lbl" width="15%" align="right">Name:</td>
  94.                     <td id="name_txt" width="15%"><input name="name_box" type="text" required></td>
  95.                     <td id="name_blank" width="15%"></td>
  96.                 </tr>
  97.                 <tr>
  98.                     <td id="street_err" width="15%"></td>
  99.                     <td class="odd" id="street_lbl" width="15%" align="right">Street:</td>
  100.                     <td class="odd" id="street_txt" width="15%"><input name="street_box" type="text" required></td>
  101.                     <td id="street_blank" width="15%"></td>
  102.                 </tr>
  103.                 <tr>
  104.                     <td id="city_err" width="15%"></td>
  105.                     <td id="city_lbl" width="15%" align="right">City:</td>
  106.                     <td id="city_txt" width="15%"><input name="city_box" type="text" required></td>
  107.                     <td id="city_blank" width="15%"></td>
  108.                 </tr>
  109.                 <tr>
  110.                     <td id="prov_err" width="15%"></td>
  111.                     <td class="odd" id="prov_lbl" width="15%" align="right">Province:</td>
  112.                     <td class="odd" id="prov_txt" width="15%">
  113.                         <select name="prov_select">
  114.                             <option value="AB">Alberta</option>
  115.                             <option value="BC">British Columbia</option>
  116.                             <option value="MB">Manitoba</option>
  117.                             <option value="NB">New Brunswick</option>
  118.                             <option value="NL">Newfoundland & Labrador</option>
  119.                             <option value="NS">Nova Scotia</option>
  120.                             <option value="NT">Northwest Territories</option>
  121.                             <option value="NU">Nunavut</option>
  122.                             <option value="ON" selected>Ontario</option>
  123.                             <option value="PE">P.E.I.</option>
  124.                             <option value="QC">Quebec</option>
  125.                             <option value="SK">Saskatchewan</option>
  126.                             <option value="YT">Yukon</option>
  127.                         </select>
  128.                     </td>
  129.                     <td id="prov_blank" width="15%"></td>
  130.                 </tr>
  131.                 <tr>
  132.                     <td id="postal_err" color="red" width="15%"></td>
  133.                     <td id="postal_lbl" width="15%" align="right">Postal Code:</td>
  134.                     <td id="postal_txt" width="15%"><input name="postalcode_box" type="text" required></td>
  135.                     <td id="postal_blank" width="15%"></td>
  136.                 </tr>
  137.                 <tr>
  138.                     <td id="type_err" width="15%"></td>
  139.                     <td class="odd" id="type_lbl" width="15%" align="right">Occupation:</td>
  140.                     <td class="odd" id="type_txt" width="15%">
  141.                         <input id="student" name="occupation" type="radio" value="student" onchange="displayOptions()" checked>Student
  142.                         <input id="faculty" name="occupation" type="radio" value="faculty" onchange="displayOptions()">Faculty
  143.                     </td>
  144.                     <td id="type_blank" width="15%"></td>
  145.                 </tr>
  146.                 <tr>
  147.                     <td id="details_err" width="15%"></td>
  148.                     <td id="details_lbl" width="15%" align="right">Additional information:</td>
  149.                     <td id="details_txt" width="15%" style="word-wrap: break-word;">
  150.                         <input name="it_check" type="checkbox" value="it">Student in Information Technology <br>
  151.                         <input name="laptop_check" type="checkbox" value="laptop">Owns a laptop<br>
  152.                         <input name="bus_check" type="checkbox" value="bus">Takes bus to school<br>
  153.                         <input name="music_check" type="checkbox" value="music">Owns a portable music player
  154.                     </td>
  155.                     <td id="details_blank" width="15%"></td>
  156.                 </tr>
  157.                 <tr>
  158.                     <td id="buttons_err" width="15%"></td>
  159.                     <td id="buttons_lbl" width="15%"></td>
  160.                     <td id="buttons_txt" width="15%" align="right">
  161.                         <input id="reset_btn" type="button" value="Reset" onclick="resetForm()">
  162.                         <input type="submit" value="Submit">
  163.                     </td>
  164.                     <td id="buttons_blank" width="15%"></td>
  165.                 </tr>
  166.             </table>
  167.         </form>
  168.     </body>
  169. </html>
Advertisement
Add Comment
Please, Sign In to add comment