<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Calculate Age</title>
<script type="text/javascript">
function CalculateAge(birthday) {
var re=/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;
if (birthday.value != '') {
if(re.test(birthday.value ))
{
birthdayDate = new Date(birthday.value);
dateNow = new Date();
var years = dateNow.getFullYear() - birthdayDate.getFullYear();
var months=dateNow.getMonth()-birthdayDate.getMonth();
var days=dateNow.getDate()-birthdayDate.getDate();
if (isNaN(years)) {
document.getElementById('lblAge').innerHTML = '';
document.getElementById('lblError').innerHTML = 'Input date is incorrect!';
return false;
}
else {
document.getElementById('lblError').innerHTML = '';
if(months
< 0 || (months == 0 && days < 0)) {
years = parseInt(years) -1;
document.getElementById('lblAge').innerHTML
= years +' Years '
}
else {
document.getElementById('lblAge').innerHTML
= years +' Years '
}
}
}
else
{
document.getElementById('lblError').innerHTML = 'Date must be mm/dd/yyyy format';
return false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Date of Birth :<asp:TextBox ID="txtAge" runat="server" onblur="CalculateAge(this)" />(mm/dd/yyyy)
<span style="color: Red">
<asp:Label ID="lblError" runat="server"></asp:Label></span>
<br />
Age : <span id="lblAge"></span>
</div>
</form>
</body>
</html>
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
Subscribe by RSS
Subscribe by Email
13 comments :
but it isnt working in master page.what to do.please help
hi,
it contains only JavaScript code it will work in any page. Please check your code i think there is mistake in your masterpage.
hi i am working on C#.Net Desktop Application.
I want to calculate person age using C#.net win application.. please help me..
My email is : chirag.makwana1989@gmail.com
Thanks In Advance.
Could you explain the regular expression in depth please?
There is a bug in your code. It only subtracts year from year, months from months, and days from days. This is not how you calculate age.
For Example: If you put a day which is greater(in value) than today's date then you get negative days as number of days.
I like your other posts but this is very wrong.
if(months < 0 || (months == 0 && days < 0)) {
years = parseInt(years) -1;
document.getElementById('lblAge').innerHTML = years;
}
else {
document.getElementById('lblAge').innerHTML = years;
}
To make this code work add this.
Thanks.
@Abhishek...
Thanks for pointing my mistake I updated code....
provide me download option for this tutorials
how to store the age in database.and how the age will increase as the year passes by.
Hi Suresh,
How to display the age within the textbox and how to display age automatically within the textbox without clicking on texbox..
months and days not calculated
Hai suresh send me fromdate less than to date in javascript