Javascript for calculating age
<script type="text/javascript">
function showAge() {
var d = document.getElementById('<%= txtDateOfBirth.ClientID %>').value.split('/');
var today = new Date();
var bday = new Date(d[2], d[1], d[0]);
var by = bday.getFullYear();
var bm = bday.getMonth() - 1;
var bd = bday.getDate();
var age = 0; var dif = bday;
while (dif <= today) {
var dif = new Date(by + age, bm, bd);
age++;
}
age += -2;
document.getElementById("<%=txtCurrentAge.ClientID %>").value = age;
alert('You are ' + age + ' years old')
}
</script>
function showAge() {
var d = document.getElementById('<%= txtDateOfBirth.ClientID %>').value.split('/');
var today = new Date();
var bday = new Date(d[2], d[1], d[0]);
var by = bday.getFullYear();
var bm = bday.getMonth() - 1;
var bd = bday.getDate();
var age = 0; var dif = bday;
while (dif <= today) {
var dif = new Date(by + age, bm, bd);
age++;
}
age += -2;
document.getElementById("<%=txtCurrentAge.ClientID %>").value = age;
alert('You are ' + age + ' years old')
}
</script>
//On page load ..
this.txtDateOfBirth.Attributes.Add("OnBlur",
"javascript:return showAge();");
No comments:
Post a Comment