-
Notifications
You must be signed in to change notification settings - Fork 0
/
application-form.html
191 lines (183 loc) · 6.03 KB
/
application-form.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
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>JavaScript Form validation</title>
<link rel="stylesheet" href="/examples/css/form-style.css" />
<script>
// Defining a function to display error message
function printError(elemId, hintMsg) {
document.getElementById(elemId).innerHTML = hintMsg;
}
// Defining a function to validate form
function validateForm() {
// Retrieving the values of form elements
var name = document.contactForm.name.value;
var email = document.contactForm.email.value;
var mobile = document.contactForm.mobile.value;
var country = document.contactForm.country.value;
var gender = document.contactForm.gender.value;
var hobbies = [];
var checkboxes = document.getElementsByName("hobbies[]");
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
// Populate hobbies array with selected values
hobbies.push(checkboxes[i].value);
}
}
// Defining error variables with a default value
var nameErr = (emailErr = mobileErr = countryErr = genderErr = true);
// Validate name
if (name == "") {
printError("nameErr", "Please enter your name");
} else {
var regex = /^[a-zA-Z\s]+$/;
if (regex.test(name) === false) {
printError("nameErr", "Please enter a valid name");
} else {
printError("nameErr", "");
nameErr = false;
}
}
// Validate email address
if (email == "") {
printError("emailErr", "Please enter your email address");
} else {
// Regular expression for basic email validation
var regex = /^\S+@\S+\.\S+$/;
if (regex.test(email) === false) {
printError("emailErr", "Please enter a valid email address");
} else {
printError("emailErr", "");
emailErr = false;
}
}
// Validate mobile number
if (mobile == "") {
printError("mobileErr", "Please enter your mobile number");
} else {
var regex = /^[1-9]\d{9}$/;
if (regex.test(mobile) === false) {
printError(
"mobileErr",
"Please enter a valid 10 digit mobile number"
);
} else {
printError("mobileErr", "");
mobileErr = false;
}
}
// Validate country
if (country == "Select") {
printError("countryErr", "Please select your country");
} else {
printError("countryErr", "");
countryErr = false;
}
// Validate gender
if (gender == "") {
printError("genderErr", "Please select your gender");
} else {
printError("genderErr", "");
genderErr = false;
}
// Prevent the form from being submitted if there are any errors
if (
(nameErr || emailErr || mobileErr || countryErr || genderErr) == true
) {
return false;
} else {
// Creating a string from input data for preview
var dataPreview =
"You've entered the following details: \n" +
"Full Name: " +
name +
"\n" +
"Email Address: " +
email +
"\n" +
"Mobile Number: " +
mobile +
"\n" +
"Country: " +
country +
"\n" +
"Gender: " +
gender +
"\n";
if (hobbies.length) {
dataPreview += "Hobbies: " + hobbies.join(", ");
}
// Display input data in a dialog box before submitting the form
alert(dataPreview);
}
}
</script>
</head>
<body>
<form
name="contactForm"
onsubmit="return validateForm()"
action="/examples/actions/confirmation.php"
method="post"
>
<h2>Application Form</h2>
<div class="row">
<label>Full Name</label>
<input type="text" name="name" />
<div class="error" id="nameErr"></div>
</div>
<div class="row">
<label>Email Address</label>
<input type="text" name="email" />
<div class="error" id="emailErr"></div>
</div>
<div class="row">
<label>Mobile Number</label>
<input type="text" name="mobile" maxlength="10" />
<div class="error" id="mobileErr"></div>
</div>
<div class="row">
<label>Country</label>
<select name="country">
<option>Select</option>
<option>Australia</option>
<option>India</option>
<option>United States</option>
<option>United Kingdom</option>
</select>
<div class="error" id="countryErr"></div>
</div>
<div class="row">
<label>Gender</label>
<div class="form-inline">
<label><input type="radio" name="gender" value="male" /> Male</label>
<label
><input type="radio" name="gender" value="female" /> Female</label
>
</div>
<div class="error" id="genderErr"></div>
</div>
<div class="row">
<label>Hobbies <i>(Optional)</i></label>
<div class="form-inline">
<label
><input type="checkbox" name="hobbies[]" value="sports" />
Sports</label
>
<label
><input type="checkbox" name="hobbies[]" value="movies" />
Movies</label
>
<label
><input type="checkbox" name="hobbies[]" value="music" />
Music</label
>
</div>
</div>
<div class="row">
<input type="submit" value="Submit" />
</div>
</form>
</body>
</html>