-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormJQuery.html
More file actions
50 lines (46 loc) · 1.21 KB
/
FormJQuery.html
File metadata and controls
50 lines (46 loc) · 1.21 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<form action="" id="form">
<input type="text" id="name" placeholder="Enter name" />
<span id="nameError"></span>
<br />
<input type="email" id="email" placeholder="Enter email" />
<span id="emailError"></span>
<br />
<button type="submit">Signup</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$("#form").on("submit", function (e) {
// e.preventDefault()
const name = $("#name").val()
const email = $("#email").val()
let nameError = (emailError = true)
if (name == "") {
printMessage("nameError", "Name is Required...")
} else {
printMessage("nameError", "")
}
if (email == "") {
printMessage("emailError", "Email is Required...")
} else {
printMessage("emailError", "")
}
if (nameError || emailError == true) {
return false
} else {
console.log(name, email)
}
})
function printMessage(el, msg) {
$("#" + el).html(msg)
}
</script>
</body>
</html>