-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegistrationForm.html
166 lines (142 loc) · 5.08 KB
/
RegistrationForm.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Registration Form</title>
<style>
* {
box-sizing:border-box;
}
form {
max-width: 400px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
input[type=text], input[type=email], input[type=tel], input[type=password] {
width: 100%;
padding: 12px;
margin: 6px 0;
box-sizing: border-box;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
.error {
color: red;
font-size: 0.6em;
display:block;
width:100%;
text-align:right;
height:1.2em;
}
.required::before {
content: "*";
color: red;
margin-right: 3px;
}
</style>
</head>
<body>
<form action="#" method="post" onsubmit="return validateForm()">
<label for="fname" class="required">First Name:</label>
<input type="text" id="fname" name="fname">
<span id="fnameError" class="error"></span>
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="lname">
<span id="lnameError" class="error"></span>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<span id="emailError" class="error"></span>
<label for="mobile">Mobile Number:</label>
<input type="text" id="mobile" name="mobile">
<span id="mobileError" class="error"></span>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<span id="passwordError" class="error"></span>
<label for="confirm_password">Confirm Password:</label>
<input type="password" id="confirm_password" name="confirm_password">
<span id="confirmPasswordError" class="error"></span>
<input type="submit" value="Register">
</form>
<script>
function validateForm() {
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var email = document.getElementById('email').value;
var mobile = document.getElementById('mobile').value;
var password = document.getElementById('password').value;
var confirmPassword = document.getElementById('confirm_password').value;
var fnameError = document.getElementById('fnameError');
var lnameError = document.getElementById('lnameError');
var emailError = document.getElementById('emailError');
var mobileError = document.getElementById('mobileError');
var passwordError = document.getElementById('passwordError');
var confirmPasswordError = document.getElementById('confirmPasswordError');
fnameError.textContent = "";
lnameError.textContent = "";
emailError.textContent = "";
mobileError.textContent = "";
passwordError.textContent = "";
confirmPasswordError.textContent = "";
var isValid = true;
if (fname.trim() === "") {
fnameError.textContent = "First name is required";
isValid = false;
}
if (lname.trim() === "") {
lnameError.textContent = "Last name is required";
isValid = false;
}
if (email.trim() === "") {
emailError.textContent = "Email is required";
isValid = false;
} else if (!isValidEmail(email)) {
emailError.textContent = "Invalid email address";
isValid = false;
}
if (mobile.trim() === "") {
mobileError.textContent = "Mobile number is required";
isValid = false;
} else if (!isValidMobile(mobile)) {
mobileError.textContent = "Invalid mobile number";
isValid = false;
}
if (password.trim() === "") {
passwordError.textContent = "Password is required";
isValid = false;
}
else if(!isValidPassword(password)){
passwordError.textContent = "Must contain at least 1 number,1 uppercase and 1 lowercase letter, and at least 8 character long";
isValid = false;
}
if (confirmPassword.trim() === "") {
confirmPasswordError.textContent = "Confirm password is required";
isValid = false;
} else if (password !== confirmPassword) {
confirmPasswordError.textContent = "Passwords do not match";
isValid = false;
}
return isValid;
}
function isValidEmail(email) {
var re = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.\.-]+\.[a-zA-Z]{2,4}$/;
return re.test(email);
}
function isValidMobile(mobile) {
var re = /^[1-9][0-9]{9}$/;
return re.test(mobile);
}
function isValidPassword(password) {
var re = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/
return re.test(password);
}
</script>
</body>
</html>