Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 5 votes, 3.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-11-2009, 06:11 AM   PM User | #1
radhika vk
New to the CF scene

 
Join Date: Jul 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
radhika vk is an unknown quantity at this point
Question Calculating age from date of birth in javascript?

Hi ,

I am using date of birth field in my application and i m taking date of birth in yyyy-mm-dd format.

I need to calculate age by using this date of birth and should show alert if age is less than 5 years.

help me....

Thanks in advance
radhika vk is offline   Reply With Quote
Old 07-11-2009, 08:01 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are:-


Code:
Enter Date Of Birth in nowyearYY-MM-DD format <input type = "text" name = "dob" id = "dob" size = "10" maxlength = "10" onblur = "checkAge()">

<script type="text/javascript"> 

function checkAge(){ 
var today = new Date(); 
var d = document.getElementById("dob").value;
if (!/\d{4}\-\d{2}\-\d{2}/.test(d)) {   // check valid format
showMessage();
return false;
}

d = d.split("-");
var byr = parseInt(d[0]); 
var nowyear = today.getFullYear();
if (byr >= nowyear || byr < 1900) {  // check valid year
showMessage();
return false;
}
var bmth = parseInt(d[1],10)-1;   // radix 10!
if (bmth <0 || bmth >11) {  // check valid month 0-11
showMessage() 
return false;
}
var bdy = parseInt(d[2],10);   // radix 10!
var dim = daysInMonth(bmth+1,byr);
if (bdy <1 || bdy > dim) {  // check valid date according to month
showMessage();
return false;
}

var age = nowyear - byr;
var nowmonth = today.getMonth();
var nowday = today.getDate();
if (bmth > nowmonth) {age = age - 1}  // next birthday not yet reached
else if (bmth == nowmonth && nowday < bdy) {age = age - 1}

alert('You are ' + age + ' years old'); 
if (age <= 15) {
alert ("You are 15 years old or less!");  
}
}

function showMessage() {
if (document.getElementById("dob").value != "") {
alert ("Invalid date format or impossible year/month/day of birth - please re-enter as nowyearYY-MM-DD");
document.getElementById("dob").value = "";
document.getElementById("dob").focus();
}
}

function daysInMonth(month,year) {  // months are 1-12
var dd = new Date(year, month, 0);
return dd.getDate();
} 

</script>
BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.


"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

Last edited by Philip M; 07-12-2009 at 03:43 PM.. Reason: Improved - checks valid date according to month
Philip M is offline   Reply With Quote
Old 09-10-2010, 03:52 AM   PM User | #3
crisstina
New to the CF scene

 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
crisstina is an unknown quantity at this point
Thank you! this script was extremely useful for me...

I just fixed a little thing about the "birthday"... if someone logs in and their birthday is the same day, then the app will allow them to register.

here's the adjusted code:



function checkAge()
{

var today = new Date();
var d = document.getElementById("dob").value;
if (!/\d{4}\-\d{2}\-\d{2}/.test(d))
{ // check valid format
showMessage();
return false;
}

d = d.split("-");
var byr = parseInt(d[0]);
var nowyear = today.getFullYear();
if (byr >= nowyear || byr < 1900)
{ // check valid year
showMessage();
return false;
}
var bmth = parseInt(d[1],10)-1; // radix 10!
if (bmth <0 || bmth >11)
{ // check valid month 0-11
showMessage();
return false;
}
var bdy = parseInt(d[2],10); // radix 10!
var dim = daysInMonth(bmth+1,byr);
if (bdy <1 || bdy > dim)
{ // check valid date according to month
showMessage();
return false;
}

var age = nowyear - byr;
var nowmonth = today.getMonth();
var nowday = today.getDate();
var age_month = nowmonth - bmth;
var age_day = nowday - bdy;
if (age < 18 )
{
alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
}
else if (age == 18 && age_month <= 0 && age_day <0)
{
alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
}


}

function showMessage()
{
if (document.getElementById("dob").value != "")
{
alert ("Invalid date format or impossible year/month/day of birth - please re-enter as YYYY-MM-DD");
document.getElementById("dob").value = "";
document.getElementById("dob").focus();
}
}

function daysInMonth(month,year) { // months are 1-12
var dd = new Date(year, month, 0);
return dd.getDate();
}
crisstina is offline   Reply With Quote
Old 06-05-2012, 08:33 PM   PM User | #4
instigator2
New to the CF scene

 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
instigator2 is an unknown quantity at this point
dob script

I decided to try this script today and it worked very well. Thank you, Phillip M for your contribution and to crisstina for your modification.

Much appreciated
instigator2 is offline   Reply With Quote
Reply

Bookmarks

Tags
age, dob, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:06 AM.


Advertisement
Log in to turn off these ads.