-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_js.html
189 lines (172 loc) · 6.34 KB
/
form_js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body{
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
padding: 20px;
border-radius: 8px;
max-width: 400px;
width:100%
}
h1{
background-color: #007bff;
text-align: center;
font-size: 20px;
color: white;
}
p{
margin-bottom: 25px;
}
fieldset{
border: 2px solid grey;
padding: 10px;
margin-bottom: 15px;
border-radius: 5px;
}
legend{
padding: 0 10px;
font-weight: bold;
}
label{
display: block;
margin-bottom: 5px;
color: #555;
}
textarea{
margin-bottom: 20px;
}
button{
background-color: #007bff;
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
font-size: 16px;
}
button:hover{
background-color: #0056b3;
}
div{
margin-left: 50%;
}
</style>
<body>
<div>
<form id="rentalForm">
<h1>Futsal Facility Rental!<br></h1>
<fieldset>
<legend>Information</legend>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname">
<span class="error" id="fnameError"></span>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname">
<span class="error" id="lnameError"></span>
<label for="age">Age:</label>
<input type="text" id="age" name="age">
<span class="error" id="ageError"></span>
</fieldset>
<p>
Date<br><br>
<input type="datetime-local" name="appointment-time" id="appointment-time">
<span class="error" id="dateError"></span>
</p>
<fieldset>
<legend>Payment</legend>
<input type="radio" id="credit" name="Payment" value="Credit">
<label for="credit">Credit card</label>
<input type="radio" id="cash" name="Payment" value="Cash">
<label for="cash">Cash</label>
<input type="radio" id="account" name="Payment" value="Account">
<label for="account">Account transfer</label><br>
<span class="error" id="paymentError"></span>
</fieldset>
<fieldset>
<legend>Need</legend>
<input type="checkbox" id="shoes" name="shoes" value="Shoes">
<label for="shoes">Futsal shoes</label><br>
<input type="checkbox" id="water" name="water" value="Water">
<label for="water">Water</label><br>
</fieldset>
Levels<br><br>
<select id="level">
<optgroup label="Levels">
<option value="first">advanced</option>
<option value="second">intermediate</option>
<option value="third">beginner</option>
</optgroup>
</select>
<br><br>Color<br><br>
<input type="color" id="color" name="color" list="list"><br>
<datalist id="list">
<option value="#ff0000">Red</option>
<option value="#00ff00">Green</option>
<option value="#0000ff">Blue</option>
<option value="#ffff00">Yellow</option>
<option value="#ff00ff">Magenta</option>
</datalist>
<br>Have to know<br>
<textarea name="textarea_content"></textarea>
<button type="submit">Submit</button>
</form>
</div>
<script>
document.getElementById("rentalForm").addEventListener("submit", function(event) {
let isValid = true;
document.getElementById("fnameError").textContent = "";
document.getElementById("lnameError").textContent = "";
document.getElementById("ageError").textContent = "";
document.getElementById("dateError").textContent = "";
document.getElementById("paymentError").textContent = "";
const fname = document.getElementById("fname").value;
const nameRegex = /^[a-zA-Z]+$/;
if (fname === ""){
document.getElementById("fnameError").textContent = "First name is required.";
isValid = false;
} else if (!nameRegex.test(fname)){
document.getElementById("fnameError").textContent = "First name must only contain letters.";
isValid = false;
}
const lname = document.getElementById("lname").value;
if (lname === ""){
document.getElementById("lnameError").textContent = "Last name is required.";
isValid = false;
} else if (!nameRegex.test(lname)){
document.getElementById("lnameError").textContent = "Last name must only contain letters.";
isValid = false;
}
const age = document.getElementById("age").value;
const ageRegex = /^[0-9]+$/;
if (age === ""){
document.getElementById("ageError").textContent = "Age is required.";
isValid = false;
} else if (!ageRegex.test(age)){
document.getElementById("ageError").textContent = "Age must be a number.";
isValid = false;
}
const appointmentTime = document.getElementById("appointment-time").value;
if (appointmentTime === ""){
document.getElementById("dateError").textContent = "Please select a date and time.";
isValid = false;
}
const payment = document.querySelector('input[name="Payment"]:checked');
if (!payment){
document.getElementById("paymentError").textContent = "Please select a payment method.";
isValid = false;
}
if (!isValid){
event.preventDefault();
}
});
</script>
</body>
</html>