i would like someone with experience in JavaScript to please look through this script and tell me what i’m doing wrong, the application of this script can be seen at jsfiddle.
The first script showAge() is supposed to get the age from the Date of Birth, and then populate the txt field “age” with the result. the second script is supposed to then parseInt the “age” field and check if the result matches any of the criteria (shown in the if..else statements) and then populate two fields (rsltf and rsltq) with points based on the if..else statements. but right now it just outputs 10 and 16 (the first values of the two if..else if statements) regardless of the age. i really cant figure it out :/
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quebec and Federal Immigration Points Calculator</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
function showAge(){
var d =document.getElementById('dob').value.split('/');
var today = new Date();
var bday = new Date(d[2],d[1],d[0]);
var age = today.getFullYear() - bday.getFullYear();
if(today.getMonth() < bday.getMonth() || (today.getMonth() == bday.getMonth() && today.getDate() < bday.getDate()))
{
t = age--;
}
else {
t = age
}
form.age.value = t;
}
</script>
<script type="text/javascript">
function compute(form)
{
var a = parseInt(form.age.value, 10) || 0;
if (21 <= a <= 49){
f = 10;
}
else if (20 == a == 50){
f = 8;
}
else if (19 == a == 51){
f = 6;
}
else if (18 == a == 52){
f = 4;
}
else if (17 == a == 53){
f = 2;
}
else if (17 > a > 53){
f = 0;
}
form.rsltf.value = f;
if (18 <= a <= 35){
q = 16;
}
else if (a == 36){
q = 14;
}
else if (a == 37){
q = 12;
}
else if (a == 38){
q = 10;
}
else if (a == 39){
q = 8;
}
else if (a == 40){
q = 6;
}
else if (a == 41){
q = 4;
}
else if (a == 42){
q = 2;
}
else if (18 > a > 43){
q = 0;
}
form.rsltq.value = q;
}
</script>
</head>
<body>
<form name="form">
<p>Your Date of Birth (format:<strong>dd/mm/yyyy</strong>)</p><br />
<input onBlur="compute(this.form)" onchange="showAge()" name="dob" id="dob" />
<p>Your Age</p><input name="age" type="text" style="font-size: 15px" value="" size="7" readonly>
<h1>Your education</h1>
<INPUT NAME="calc" VALUE="Calculate" TYPE="button" onClick="compute(this.form)"><br />
<input name="rsltf" type="text" style="font-size: 50px" value="" size="20" readonly /><br />
<input name="rsltq" type="text" style="font-size: 50px" value="" size="20" readonly />
</form>
</body>
</html>
This code will not work at all:
JavaScript will interpret this like so:
You need to be verbose:
What about this code:
What is it supposed to do?