Skip to content

Commit f26964e

Browse files
Charlene TranCharlene Tran
Charlene Tran
authored and
Charlene Tran
committed
CT completed
1 parent 74ff6d4 commit f26964e

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

user-input-with-forms/chapter-examples-CT/form-validation/index.html

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,77 @@
1010
</head>
1111

1212

13-
<script>
13+
<!-- ORIGINAL Exampler -->
14+
<!-- <script>
1415
window.addEventListener("load", function() {
1516
let form = document.querySelector("form");
17+
1618
form.addEventListener("submit", function(event) {
1719
alert("submit clicked");
1820
});
1921
});
22+
</script> -->
23+
24+
25+
<!-- Add Validation - Get Reference to Inputs -->
26+
<!-- <script>
27+
window.addEventListener("load", function() {
28+
let form = document.querySelector("form");
29+
30+
form.addEventListener("submit", function(event) {
31+
let usernameInput = document.querySelector("input[name=username]");
32+
// Alert the Current Value found in the Username Input
33+
alert("username: " + usernameInput.value);
34+
});
35+
});
36+
</script> -->
37+
38+
<!-- Add Validation - Alert the Input Values When Submitted -->
39+
<!-- <script>
40+
window.addEventListener("load", function() {
41+
let form = document.querySelector("form");
42+
43+
form.addEventListener("submit", function(event) {
44+
let usernameInput = document.querySelector("input[name=username]");
45+
let teamName = document.querySelector("input[name=team]");
46+
47+
48+
// Uses a Conditional statement to verify ALL fields are filled
49+
if (usernameInput.value === "" || teamName.value === "") {
50+
alert("ALL fields are required!");
51+
}
52+
});
53+
});
54+
</script> -->
55+
56+
<!-- PREVENT Form Submission until ALL inputs have Valid values -->
57+
<script>
58+
window.addEventListener("load", function() {
59+
let form = document.querySelector("form");
60+
61+
form.addEventListener("submit", function(event) {
62+
let usernameInput = document.querySelector("input[name=username]");
63+
let teamName = document.querySelector("input[name=team]");
64+
65+
66+
// Uses a Conditional statement to verify ALL fields are filled
67+
if (usernameInput.value === "" || teamName.value === "") {
68+
alert("ALL fields are required!");
69+
70+
// STOPS the Form Submission
71+
event.preventDefault();
72+
}
73+
});
74+
});
2075
</script>
2176

2277
<body>
2378
<form
2479
method="POST"
2580
action="https://handlers.education.launchcode.org/request-parrot">
2681

27-
<label>Username </label>
28-
<input type="text" name="username">
82+
<label>Username <input type="text" name="username"></label>
83+
2984

3085
<label>Team Name <input type="text" name="team"></label>
3186
<button>Submit</button>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Check Input Value with DOM</title>
7+
</head>
8+
9+
<body>
10+
<form>
11+
<label>Language
12+
<input type="text" name="language" id="language" value="JavaScript">
13+
</label>
14+
</form>
15+
16+
<button id="update">Update Input Value</button>
17+
18+
<script>
19+
let button = document.getElementById("update");
20+
21+
// Add Event Handler for when button clicked
22+
button.addEventListener("click", function() {
23+
let input = document.getElementById("language");
24+
console.log(input.value);
25+
26+
// Now Update the value in the Input
27+
input.value = input.value + " rocks!";
28+
console.log(input.value);
29+
});
30+
</script>
31+
</body>
32+
</html>

0 commit comments

Comments
 (0)