In: Computer Science
Create a form that has the following fields:
Create two arrays of objects, one for US States, one for Canadian Provinces. The object should contain Name and Abbreviation. You don’t need to do all the states or provinces, just a few to make sure your code works. (3 OF EACH)
When the user selects either US or Canada, populate the drop down using the array of object. The first, default option should be “Please Select State” or “Please Select Province” depending on the selected country.
On Submitting the form, validate all form fields. Validate the Postal Code according to the rules for the selected country… US or Canada. You should do JavaScript Validation even if you use HTML5 controls
Follow good practices for JavaScript
Use the appropriate form controls from all the available form controls – HTML and HTML5.
Make the form accessible by using labels.
THIS IS TO DONE USING HTML/HTML5 AND JAVASCRIPT
<!doctype html> <html> <head></head> <body> <form onsubmit="event.preventDefault(); validate(this)" action="#"> <table> <tr> <td> <label for="Name">Name:</label> </td> <td> <input type="text" name="Name" class="Name" id="Name" value=""> </td> </tr> <tr> <td> <label for="Name">Address:</label> </td> <td> <input type="text" name="Address" class="Address" id="Address" value=""> </td> </tr> <tr> <td> <label for="Name">City:</label> </td> <td> <input type="text" name="City" class="City" id="City" value=""> </td> </tr> <tr> <td> <label for="Country">Country:</label> </td> <td> <select class="Country" name="Country" id="Country" onchange="populate_state(this.value)"> <option value="0">Please Select Country</option> <option value="1">US</option> <option value="2">Canada</option> </select> </td> </tr> <tr> <td> <label for="State">State/Province:</label> </td> <td> <select class="State" name="State" id="State"> <option value="0">Please Select State</option> </select> </td> </tr> <tr> <td> <label for="Code">Postal Code:</label> </td> <td> <input type="text" name="Code" class="Code" id="Code" value=""> </td> </tr> <tr> <td> <input type="submit" name="" value="Submit"> </td> </tr> </table> </form> <p id="Error" ></p> <script type="text/javascript"> us = [ {name: "Albama", abbr: "AL"}, {name: "Alaska", abbr: "AK"}, {name: "California", abbr: "CA"} ] canada = [ {name: "Alberta", abbr: "AB"}, {name: "British Columbia", abbr: "BC"}, {name: "Manitoba", abbr: "MB"} ] function populate(obj) { let select = document.getElementById("State"); select.options.length = 0; var el = document.createElement("option"); el.textContent = "Please Select State"; el.value = 0; select.appendChild(el); obj.forEach(o => { var el = document.createElement("option"); el.textContent = o.name; el.value = o.abbr; select.appendChild(el); }); } function populate_state(value) { if(value == 0) { return; } else if (value == 1) { populate(us); } else if (value == 2) { populate(canada); } } function validate() { let name = document.getElementById("Name").value; let address = document.getElementById("Address").value; let city = document.getElementById("City").value; let country = document.getElementById("Country").selectedIndex; let state = document.getElementById("State").selectedIndex; let code = document.getElementById("Code").value; let error = document.getElementById("Error"); if (!name) { error.innerHTML = "Name required"; } else if (!address) { error.innerHTML = "Address required"; } else if (!city) { error.innerHTML = "City required"; } else if (country == 0) { error.innerHTML = "Select Country"; } else if (state == 0) { error.innerHTML = "Select State"; } else if ((country == 1 && code.length != 5) || (country == 2 && code.length != 6)) { error.innerHTML = "Enter Corrent Country Code (US: 5 characters | Canada: 6 characters)"; } else { error.innerHTML = "No Error : Form Varified."; } } </script> </body> </html>