-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contact Form.html
135 lines (127 loc) · 4.35 KB
/
Contact 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
<style>
.contact-form {
max-width: 500px;
margin: 0 auto;
padding: 20px;
background-color: #f9f9f9;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="email"],
textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
textarea {
height: 100px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.error {
color: red;
font-size: 0.9em;
margin-top: 5px;
}
</style>
</head>
<body>
<form id="contactForm" class="contact-form">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<div class="error" id="nameError"></div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<div class="error" id="emailError"></div>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
<div class="error" id="messageError"></div>
</div>
<button type="submit">Send Message</button>
</form>
<script>
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('contactForm');
const nameInput = document.getElementById('name');
const emailInput = document.getElementById('email');
const messageInput = document.getElementById('message');
const nameError = document.getElementById('nameError');
const emailError = document.getElementById('emailError');
const messageError = document.getElementById('messageError');
form.addEventListener('submit', function(e) {
e.preventDefault();
if (validateForm()) {
// Form is valid, you can send the data to your server here
console.log('Form submitted:', {
name: nameInput.value,
email: emailInput.value,
message: messageInput.value
});
// Reset form after submission
form.reset();
}
});
function validateForm() {
let isValid = true;
if (nameInput.value.trim() === '') {
nameError.textContent = 'Name is required';
isValid = false;
} else {
nameError.textContent = '';
}
if (emailInput.value.trim() === '') {
emailError.textContent = 'Email is required';
isValid = false;
} else if (!isValidEmail(emailInput.value)) {
emailError.textContent = 'Please enter a valid email address';
isValid = false;
} else {
emailError.textContent = '';
}
if (messageInput.value.trim() === '') {
messageError.textContent = 'Message is required';
isValid = false;
} else {
messageError.textContent = '';
}
return isValid;
}
function isValidEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
});
</script>
</body>
</html>