|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | | - <head> |
4 | | - <meta charset="utf-8" /> |
5 | | - <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
6 | | - <title>My form exercise</title> |
7 | | - <meta name="description" content="" /> |
8 | | - <meta name="viewport" content="width=device-width, initial-scale=1" /> |
9 | | - </head> |
10 | | - <body> |
11 | | - <header> |
12 | | - <h1>Product Pick</h1> |
13 | | - </header> |
14 | | - <main> |
15 | | - <form> |
16 | | - <!-- write your html here--> |
17 | | - <!-- |
18 | | - try writing out the requirements first as comments |
19 | | - this will also help you fill in your PR message later--> |
20 | | - </form> |
21 | | - </main> |
22 | | - <footer> |
23 | | - <!-- change to your name--> |
24 | | - <h2>By HOMEWORK SOLUTION</h2> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>T-Shirt Order</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | +<form id="tshirtForm"> |
| 10 | + <label> |
| 11 | + Full Name: |
| 12 | + <input type="text" id="name"> |
| 13 | + </label> |
| 14 | + <br><br> |
| 15 | + |
| 16 | + <label> |
| 17 | + Email: |
| 18 | + <input type="text" id="email"> |
| 19 | + </label> |
| 20 | + <br><br> |
| 21 | + |
| 22 | + <label> |
| 23 | + T-shirt Colour: |
| 24 | + <select id="colour"> |
| 25 | + <option value="">Select a colour</option> |
| 26 | + <option value="red">Red</option> |
| 27 | + <option value="blue">Blue</option> |
| 28 | + <option value="black">Black</option> |
| 29 | + </select> |
| 30 | + </label> |
| 31 | + <br><br> |
| 32 | + |
| 33 | + <fieldset> |
| 34 | + <legend>T-shirt Size</legend> |
| 35 | + <label><input type="radio" name="size" value="XS"> XS</label><br> |
| 36 | + <label><input type="radio" name="size" value="S"> S</label><br> |
| 37 | + <label><input type="radio" name="size" value="M"> M</label><br> |
| 38 | + <label><input type="radio" name="size" value="L"> L</label><br> |
| 39 | + <label><input type="radio" name="size" value="XL"> XL</label><br> |
| 40 | + <label><input type="radio" name="size" value="XXL"> XXL</label> |
| 41 | + </fieldset> |
| 42 | + |
| 43 | + <br> |
| 44 | + <button type="submit">Submit</button> |
| 45 | +</form> |
| 46 | + |
| 47 | +<script> |
| 48 | +document.getElementById("tshirtForm").addEventListener("submit", function (event) { |
| 49 | + event.preventDefault(); |
| 50 | + |
| 51 | + const name = document.getElementById("name").value.trim(); |
| 52 | + const email = document.getElementById("email").value.trim(); |
| 53 | + const colour = document.getElementById("colour").value; |
| 54 | + const size = document.querySelector('input[name="size"]:checked'); |
| 55 | + |
| 56 | + const namePattern = /^[A-Za-z\s]{2,}$/; //I added this to validate names to accept only letter and spaces |
| 57 | + const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // Basic email pattern validation |
| 58 | + |
| 59 | + if (!namePattern.test(name)) { |
| 60 | + alert("Please enter a valid name (letters and spaces only)."); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (!emailPattern.test(email)) { |
| 65 | + alert("Please enter a valid email address."); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (colour === "") { |
| 70 | + alert("Please select a t-shirt colour."); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (!size) { |
| 75 | + alert("Please select a t-shirt size."); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + alert("Order details are valid!"); |
| 80 | +}); |
| 81 | +</script> |
| 82 | + |
| 83 | +<footer> |
| 84 | + <h2>By Shuheda Begum</h2> |
25 | 85 | </footer> |
26 | | - </body> |
| 86 | + |
| 87 | +</body> |
27 | 88 | </html> |
0 commit comments