-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical_12.html
More file actions
42 lines (36 loc) · 1.05 KB
/
Copy pathpractical_12.html
File metadata and controls
42 lines (36 loc) · 1.05 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
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<form onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
<script>
function validateForm() {
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
// Basic name validation (only letters and spaces allowed)
let nameRegex = /^[a-zA-Z ]+$/;
if (!nameRegex.test(name)) {
alert("Invalid name. Please use only letters and spaces.");
return false;
}
// Basic email validation
let emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
alert("Invalid email address.");
return false;
}
// Form is valid, submit it
alert("Form submitted successfully!");
return true;
}
</script>
</body>
</html>