Here I will explain how to calculate age of person from date of birth and regular expression for mm/dd/yyyy format using JavaScript.
Description
In some situation we need to display age of person based on date of birth .In that situation we can calculate age of person using JavaScript by comparing present date and date of birth as shown below. Here I have a one textbox every time I need to validate that data whether user enter valid data or not if the date not in mm/dd/yyyy format or not.
Regular expression for mm/dd/yyyy date format is
re=/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;
If you want to implement code for calculate age of person using javascript design your aspx page like this
<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>
|
Demo
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. |
|||
|
|||
18 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
it's not display month and day
please upload update code..
$(function() {
$('#bdate').datepicker({
onClose: function() {
var birthday = $(this).datepicker('getDate');
var today = new Date(),
age = (
(today.getMonth() > birthday.getMonth())
||
(today.getMonth() == birthday.getMonth() && today.getDate() >= birthday.getDate())
) ? today.getFullYear() - birthday.getFullYear() : today.getFullYear() - birthday.getFullYear()-1;
var months=today.getMonth()-birthday.getMonth();
if(months<0) months+= 11;
var days=today.getDate()-birthday.getDate();
if(days<0){
birthday.setMonth(birthday.getMonth()+1, 0);
days= birthday.getDate()-birthday.getDate()+today.getDate();
--months;
}
alert(months);
alert(days);
$('#age').val(age);
// alert("Age: " + age);
//alert(dayDiff);
}
});
});
function checkpassword()
{
var pass2 = document.getElementById("pass").value;
var cpass1 =document.getElementById("conpass").value;
if(pass2 === cpass1)
{
$('#cpass').html("");
}
else
{
document.getElementById("cpass").innerHTML="wrong password";
document.getElementById("conpass").focus();
}
}
function usercheck()
{
alert('hi');
var name = document.getElementById("uname").value;
if(name=="")
{
document.getElementById("disp").innerHTML="";
}
else
{
$.ajax({
type:"POST",
url:"checkuser.php",
dataType: "html",
data:{"name":name},
success: function(html){
$('#disp').html(html);
}
});
return false;
}
}
Dear Sir,
Display result is wrong from 28 Feb and 1st March to till date. if you have any correct code please send me on my email: maclibrary40@gmail.com.
Thanks in advance,
From: Lakhpat Singh
Hi i want to find difference between from date and to date in asp.net c# please help me without using timespan and javascript
can you help me include the time from present to the time added
Note: Only a member of this blog may post a comment.