-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path24.html
More file actions
55 lines (46 loc) · 1.74 KB
/
Copy path24.html
File metadata and controls
55 lines (46 loc) · 1.74 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
</head>
<body>
<h1>Form Validation</h1>
<form onsubmit="validateForm(event)">
<label for="name">Name:</label>
<input type="text" id="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" required><br><br>
<input type="submit" value="Submit">
</form>
<div id="result"></div>
<script>
function validateForm(event) {
event.preventDefault();
const name = document.getElementById('name').value.trim();
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value.trim();
const resultDiv = document.getElementById('result');
if (name === '' || email === '' || password === '') {
resultDiv.textContent = 'All fields are required.';
return;
}
if (!isValidEmail(email)) {
resultDiv.textContent = 'Please enter a valid email address.';
return;
}
resultDiv.textContent = `Form submitted successfully:
Name: ${name}
Email: ${email}
Password: ${password}`;
}
function isValidEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
</script>
</body>
</html>