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

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-21-2010, 03:42 AM   PM User | #1
coolecho
New to the CF scene

 
Join Date: Apr 2010
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
coolecho is an unknown quantity at this point
Using pull downs and calc birthdate

Hello,

I am trying to use select options to get birthdate and calculate the age. I got the birthday with "birthDay = document.form1.selDate;" However i'm lost on the birthMonth and birthYear. I've looked at lots of examples. The birthMonth probably needs to be converted to integer, or use 1 thru 12 rather than Jan thru Dec. But birthYear i'd think would come back as a valid number. Is anyone familiar with this?

Thanks.

Code:
<html>
<head>
<title>Main</title>
<script language="javascript">
<!--
function Age()
{
    var birthDay;
    var birthMonth;
    var birthYear;
    var userAge;
    var currentDay = new Date();
    var dayNow = currentDay.getDate();
    var monthNow = (currentDay.getMonth());
    var yearNow = (currentDay.getFullYear());

    birthDay = document.form1.selDate;
    
    // Don't know how to get birthmonth????
//  birthMonth = (parseInt(document.form1.selMonth.value)-1);
    
    birthYear = document.form1.selYear;
    // alert("birthyear " + birthYear); ( gives "birthYear [object HTMLSelectElement]")

    if((monthNow > birthMonth)||(monthNow == birthMonth & dayNow >= birthDay))
    { userAge = birthYear; }
    else {    userAge=birthYear+1;}
    alert("As of today, " + currentDay +' \n'+", you are: " + (yearNow - userAge) + " years old");
}
//-->
</script>
</head>
<body>
<form name="form1" id="startOfForm">
<table><tbody>
    <tr>
        <td class="question"><label for="birthdate">Enter Birthdate</label></td>
        <td>
            <select name=selMonth>
            <option value=0 selected>Month</option>    <option selected value="Jan">Jan</option>
            <option selected value="Feb">Feb</option> <option selected value="Mar">Mar</option>
            <option selected value="Apr">Apr</option> <option selected value="May">May</option>
            <option selected value="Jun">Jun</option> <option selected value="Jul">Jul</option>
            <option selected value="Aug">Aug</option> <option selected value="Sept">Sept</option>
            <option selected value="Oct">Oct</option> <option selected value="Nov">Nov</option>
            <option selected value="Dec">Dec</option>
            </select>
            <select name=selDate>
            <option value=0 selected>Date</option> <option selected value="1">1</option>
            <option selected value="2">2</option> <option selected value="3">3</option>
            <option selected value="4">4</option> <option selected value="5">5</option>
            <option selected value="6">6</option> <option selected value="7">7</option>
            <option selected value="8">8</option> <option selected value="9">9</option>
            </select>
            <select name=selYear>
            <option value=0 selected>Year</option> <option value="2001">2001</option>
            <option value="2000">2000</option> <option value="1999">1999</option>
            <option value="1998">1998</option> <option value="1997">1997</option>
            <option value="1996">1996</option> <option value="1995">1995</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
        <input name="cmd" type=button value="Age"
                onclick="Age()" style="width:150px; height:30px;">
        </td>
    </tr>
</tbody></table>
</form>
</body>
</html>
coolecho is offline   Reply With Quote
Old 04-21-2010, 04:02 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
as a Rule of Thumb, anything you get from a form control is a string, so you have to take care if you use the + operator (it is used for addition and string concatenation).

you can make your life easier in the month case:
PHP Code:
<select name="selMonth">
    <
option value="0">Jan</option>
    <
option value="1">Feb</option>
    <
option value="2">Mar</option>
// ...
</select
in the end, you just define a new Date object.
PHP Code:
var day  document.form1.selDate.value;
var 
mon  document.form1.selMonth.value;
var 
year document.form1.selYear.value;
var 
Birthday = new Date(yearmonday); 
after that you only need to calculate the year portion out of the Date difference.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
coolecho (04-21-2010)
Old 04-21-2010, 04:33 AM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Or check your other forum submissions ... see: http://www.webdeveloper.com/forum/sh...d.php?t=228388
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
coolecho (04-21-2010)
Old 04-21-2010, 03:18 PM   PM User | #4
coolecho
New to the CF scene

 
Join Date: Apr 2010
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
coolecho is an unknown quantity at this point
Hello,

Sorry about the cross post. The .value worked great, and changing month selects to numbers as well.

thanks
coolecho is offline   Reply With Quote
Reply

Bookmarks

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 02:09 PM.


Advertisement
Log in to turn off these ads.