-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathformvalidation.html
40 lines (37 loc) · 1.22 KB
/
formvalidation.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
<!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>
<form id="myForm" onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br><br>
<label for="email">Email:</label>
<input type="text" id="email" name="email">
<br><br>
<input type="submit" value="Submit">
</form>
<script>
function validateForm() {
// Get form values
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
// Regular expression for email validation
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
// Validate name and email
if (name === "" || email === "") {
alert("Both fields must be filled out");
return false;
} else if (!email.match(emailRegex)) {
alert("Invalid email address");
return false;
}
return true;
}
</script>
</body>
</html>