-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.html
148 lines (121 loc) · 4.87 KB
/
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form with Validation</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin-top: 300px;
}
.container {
background-color:white;
padding: 20px;
border-radius: 8px;
}
label {
display: block;
margin-bottom: 8px;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 15px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.error {
color:red;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<form id="registrationForm" onsubmit="return validatepassword() && validatename()">
<h1>Register your account</h1>
<label for="email">E-mail:</label>
<input type="email" id="email" name="email" pattern="^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$" required>
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" required>
<span class="error-message" id="firstNameError"></span>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName" required>
<span class="error-message" id="lastNameError"></span>
<label for="companyName">Company Name:</label>
<input type="text" id="companyName" name="companyName" required>
<label for="phone">Contact No:</label>
<input type="tel" id="phone" name="phone" pattern="^[2-9]\d{9}$" required>
<label for="streetAddress">Street Address:</label>
<input type="text" id="streetAddress" name="streetAddress" required>
<label for="city">City:</label>
<input type="text" id="city" name="city" required>
<label for="state">State:</label>
<input type="text" id="state" name="state" required>
<label for="country">Country:</label>
<input type="text" id="country" name="country" required>
<label for="zipCode">Zip Code:</label>
<input type="text" id="zipCode" name="zipCode" pattern="[0-9]{6}" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" pattern="^(?!.*(?:password|firstName|lastName)).*$" required>
<p class="error">Note: Password doesn't allow Username, First Name, or Last Name.</p>
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" name="confirmPassword" required>
<button type="submit">Register</button>
</form>
</div>
<script>
function validatepassword() {
var password = document.getElementById("password").value;
var confirmPassword = document.getElementById("confirmPassword").value;
if (password !== confirmPassword) {
alert("Password and Confirm Password do not match.");
return false;
}
return true;
}
function validatename(){
var firstNameInput = document.getElementById("firstName");
var lastNameInput = document.getElementById("lastName");
var firstNameError = document.getElementById("firstNameError");
var lastNameError = document.getElementById("lastNameError");
// Reset previous error messages
firstNameError.textContent = "";
lastNameError.textContent = "";
// Validate first name
if (!isValidName(firstNameInput.value)) {
firstNameError.textContent = "Please enter a valid first name.";
return false;
}
// Validate last name
if (!isValidName(lastNameInput.value)) {
lastNameError.textContent = "Please enter a valid last name.";
return false;
}
alert("Form submitted successfully!");
return true;
}
function isValidName(name) {
// Simple name validation using a regular expression
var namePattern = /^[A-Za-z]+$/;
return namePattern.test(name);
}
</script>
</body>
</html>