Loading ...

Caculate age from Date of birth using Javascript | CodeAsp.Net

Caculate age from Date of birth using Javascript

 /5
0 (0votes)

In this blog I will show you how to calculate age from date of birth on textbox on blur event with the help of JavaScript. Where by you just have to type your DOB in (mm/dd/yyyy) format. I have used blur event in the following code. You can call Caculate age on click or any other event as well.

 

While calculating age user tends to put wrong date format in texbox , to aviod any error i have used isNaN method.

which returns boolean. If there is an error it will return true else false. So i have used alert to show the error.

You can customize the following code as per you need.

  

Calculate Age

Here is the Code Snippet  -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Calculate Age</title>

    <script type="text/ecmascript">

        function CalculateAge(DOB) {

         if (DOB.value != '') {

            now = new Date()

            dob = DOB.value.split('/');

            if (dob.length === 3) {

                born = new Date(dob[2], dob[1] * 1 - 1, dob[0]);

                age = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));

                if(isNaN(age))

                {

                   document.getElementById('lblAge').innerHTML='';

                   alert('Input date is incorrect!');

                }

                else

                {

                 document.getElementById('lblAge').innerHTML = age;

                }

            }

        }

    }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

       Date of Birth :<input type="text" id="txtAge" onblur="CalculateAge(this)" /> (mm/dd/yyyy)

        <br />

        Age&nbsp;&nbsp;&nbsp; : <span id="lblAge"> </span>

    </div>

    </form>

</body>

</html>

 

 

 

Leave you valuable comments and suggestions. Any pointer or suggestion will be really helpful.

Happy Coding Cool !

Comments (6)

   
hajan
Well, nice post.I have shorter solution.Example:        function calculateAge(birthday)        {            if (birthday.value!='')            {                                birthdayDate = new Date(birthday.value);                dateNow = new Date();                var result = dateNow.getFullYear() - birthdayDate.getFullYear();                                alert(result);            }        } The only thing needed here is just to calculate if the current day/month is smaller then birthday day/month and to add result-1.Regards,Hajan
6/24/2010
 · 
by
   
shaitender
Thanks hanjan for your input. let me try that one too   
6/25/2010
 · 
by
   
Bashir
Bashir
the above code is good but have some error for that reason wrong output is come.........Wrong is semicolon absent here : now = new Date();
7/29/2010
 · 
by
   
Bashir
Bashir
the above code is good but have some error for that reason wrong output is come.........Wrong is semicolon absent here : now = new Date();
7/29/2010
 · 
by
   
Jackson
Jackson
Hi,can you alter the code to accept date in dd/mm/yyyy format ?
1/5/2011
 · 
by
   
Shaitender
Just Change this line  born = new Date(dob[2], dob[1] * 1 - 1, dob[0]);Toborn = new Date(dob[2], dob[0], dob[1]);date funtion in javascripts  - new date(year,month,day)as you can see the code i have splited  dob = DOB.value.split('/'); date from the texbox and passed the values according to the date function new date(year,month,day) . i hope this will help u .Thanks,ShaitenderCheck http://www.w3schools.com/jS/js_obj_date.asp
1/5/2011
 · 
by
  • :*
  • :*
  • :
 *

Top Posts