-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.js
More file actions
30 lines (27 loc) · 831 Bytes
/
Copy pathvalidate.js
File metadata and controls
30 lines (27 loc) · 831 Bytes
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
function validateForm() {
let valid = true;
// check birthdate
valid = validateBirthdate();
// // alert(valid ? "Over 18" : "Under 18");
// check favorite number
valid = valid ? validateFavoriteNumber() : false;
// // alert(valid ? "In range" : "Out of range");
return valid
}
function validateBirthdate() {
// get current date
let currentDate = new Date();
// get input date
let input = document.getElementById("birthdate").value;
let birthdate = new Date(input);
// return if age is over 18
let diff = new Date(currentDate - birthdate)
let age = Math.abs(diff.getUTCFullYear() - 1970);
return age >= 18;
}
function validateFavoriteNumber() {
// get input number
let input = document.getElementById("favorite-number").value;
// return if number between 1 and 100
return input >= 1 && input <= 100;
}