-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent Registration Form
139 lines (129 loc) · 5.02 KB
/
Student Registration Form
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Registration</title>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
.form-group {
margin-bottom: 15px;
}
label {
font-weight: bold;
}
input[type="text"], input[type="email"], input[type="tel"], input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
.error {
color: red;
}
</style>
</head>
<body>
<div class="container">
<h2>Student Registration</h2>
<form id="registrationForm">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="phone">Phone Number:</label>
<input type="tel" id="phone" name="phone" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" name="confirmPassword" required>
</div>
<div class="form-group">
<button type="submit">Register</button>
</div>
</form>
<div id="errorMessages" class="error"></div>
</div>
<script>
const form = document.getElementById("registrationForm");
const errorMessages = document.getElementById("errorMessages");
form.addEventListener("submit", function(event) {
event.preventDefault();
errorMessages.innerHTML = "";
const username = form.username.value.trim();
const email = form.email.value.trim();
const phone = form.phone.value.trim();
const password = form.password.value;
const confirmPassword = form.confirmPassword.value;
// Validation functions
function isNotEmpty(value, field) {
if (value === "") {
errorMessages.innerHTML += `${field} is required.<br>`;
return false;
}
return true;
}
function isValidPhoneNumber(phone) {
const phonePattern = /^\d{10}$/;
if (!phonePattern.test(phone)) {
errorMessages.innerHTML += "Phone number must be 10 digits.<br>";
return false;
}
return true;
}
function isValidPassword(password) {
const passwordPattern = /^(?=.*[A-Z])(?=.*\d)(?=.*[@#$&]).{7,}$/;
if (!passwordPattern.test(password)) {
errorMessages.innerHTML += "Password must be at least 7 characters long and contain one uppercase letter, one digit, and one special character (@, #, $, or &).<br>";
return false;
}
return true;
}
function passwordsMatch(password, confirmPassword) {
if (password !== confirmPassword) {
errorMessages.innerHTML += "Passwords do not match.<br>";
return false;
}
return true;
}
function isValidEmail(email) {
const emailPattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
if (!emailPattern.test(email)) {
errorMessages.innerHTML += "Invalid email address.<br>";
return false;
}
return true;
}
// Perform validations
const isUsernameValid = isNotEmpty(username, "Username");
const isEmailValid = isValidEmail(email);
const isPhoneValid = isValidPhoneNumber(phone);
const isPasswordValid = isValidPassword(password);
const doPasswordsMatch = passwordsMatch(password, confirmPassword);
if (isUsernameValid && isEmailValid && isPhoneValid && isPasswordValid && doPasswordsMatch) {
// Form is valid - you can submit the data to the server or perform further actions here
alert("Registration successful!");
form.reset();
}
});
</script>
</body>
</html>