I am very new to Javascript so please be patient. I have a script that is to check if certain form elements are null. If any of the form elements are null, an alert should be shown. However, nothing happens. I have looked at a copious amount of examples, cannot seem to get past this problem.
The script in question is shown below:
var age;
var todaysDate;
var birthDate;
var inputOk;
function setDates() {
if (d.value !== null && m.value !== null && y.value !== null) {
inputOk = true;
var day = d.value;
var month = m.value + 1;
var year = y.value;
var today = new Date();
var dob = new Date(year, month, day);
todaysDate = today.valueOf();
birthDate = dob.valueOf();
}
else {
inputOk = false;
}
}
function calculateAge() {
}
function outputAge() {
//var output = document.getElementById('ageOut');
//document.write('You are aged: '+age);
}
function getAge() {
setDates();
if (inputOk) {
calculateAge();
outputAge();
}
else {
alert("Please enter your D.O.B correctly.");
}
}
The HTML form in question is shown below:
<form name ="dob" id="dobform">
<select id="d">
<option value="null">Day</option>
</select>
<select id="m">
<option value="null">Month</option>
</select>
<select id="y">
<option value="null">Year</option>
</select>
<input type="button" value="Get my age!" onclick="getAge();"/>
</form>
Accessing your dom elements directly by id is a bad idea. I believe it will work on some versions of IE, but the standard way is to call
document.getElementById("idOfYourControl")So to get the value of your “m” select:
So your function would look like: