I have two HTML form fields in my application, one displays a dropdown for users to select if they are over 18 and the second field is for parent’s name.
I want to make the parent_name
field required
only if the user is under 18. Here’s my current code for the appropriate form parts.
JavaScript
x
13
13
1
<div>
2
<label for="over_18">Over 18?</label>
3
<select id="over_18" required multiple>
4
<option value="Yes">Yes</option>
5
<option value="No">No</option>
6
</select>
7
</div>
8
9
<div class="form-group">
10
<label>Parent Name</label>
11
<input type="parent_name" id="parent_name" required>
12
</div>
13
I’ve created the following function so far, but I’m unable to understand how to use it.
JavaScript
1
9
1
function updateRequirements() {
2
var over_18 = document.getElementById('over_18').value;
3
if (over_18 = 'No') {
4
doccument.getElementById('parent_name').required = true;
5
} else {
6
doccument.getElementById('parent_name').required = false;
7
}
8
}
9
Update:
How to achieve the same result if the first input is of type date
? So, now the second input is required only if the age provided in the first input is less than 18.
Advertisement
Answer
You can update the required
attribute of the parent_name
input every time the over_18
selection changes.
Few notes:
type="parent_name"
is invalid, it should betype="text"
.- I think you’ve added
multiple
onselect
by mistake, it doesn’t make any sense here. - I’ve also made the initial option for
select
to beSelect Age
withvalue=""
, so now you don’t needrequired
onparent_name
in your HTML.
JavaScript
1
16
16
1
const select = document.getElementById("over_18");
2
const parentInput = document.getElementById("parent_name");
3
const form = document.querySelector("form");
4
5
select.addEventListener("change", (e) => {
6
if (e.target.value === "No") {
7
parentInput.required = false;
8
} else {
9
parentInput.required = true;
10
}
11
});
12
13
form.addEventListener("submit", (e) => {
14
e.preventDefault();
15
console.log("Submitted");
16
});
JavaScript
1
15
15
1
<form>
2
<div>
3
<label for="over_18">Over 18? </label>
4
<select id="over_18" required>
5
<option value="">Select Age</option>
6
<option value="Yes">Yes</option>
7
<option value="No">No</option>
8
</select>
9
</div>
10
<div>
11
<label for="parent_name">Parent Name</label>
12
<input type="input" id="parent_name">
13
</div>
14
<button type="submit">Submit</button>
15
</form>
Update based on OP’s comment:
If age
is a date
input:
- Calculate if the age is above 18 by comparing the year, month and date of the provided date with the current date.
- Then, toggle the
required
attribute accordingly.
JavaScript
1
31
31
1
const ageInput = document.getElementById("age");
2
const parentInput = document.getElementById("parent_name");
3
const form = document.querySelector("form");
4
5
ageInput.addEventListener("change", (e) => {
6
const date = new Date(e.target.value),
7
birthYear = date.getFullYear(),
8
birthMonth = date.getMonth(),
9
birthDate = date.getDate(),
10
currYear = new Date().getFullYear(),
11
currMonth = new Date().getMonth(),
12
currDate = new Date().getDate(),
13
years = currYear - birthYear,
14
isAdult =
15
years > 18 ||
16
(years === 18 && currMonth > birthMonth) ||
17
(years === 18 && currMonth === birthMonth && currDate >= birthDate);
18
19
console.log("Is Adult?", isAdult);
20
21
if (isAdult) {
22
parentInput.required = false;
23
} else {
24
parentInput.required = true;
25
}
26
});
27
28
form.addEventListener("submit", (e) => {
29
e.preventDefault();
30
console.log("Submitted");
31
});
JavaScript
1
11
11
1
<form>
2
<div>
3
<label for="age">Age</label>
4
<input type="date" id="age" required>
5
</div>
6
<div class="form-group">
7
<label for="parent_name">Parent Name</label>
8
<input type="text" id="parent_name">
9
</div>
10
<button type="submit">Submit</button>
11
</form>