www.webdeveloper.com
Results 1 to 3 of 3

Thread: HELP!!!!! Javascript Code

  1. #1
    Join Date
    May 2011
    Posts
    2

    HELP!!!!! Javascript Code

    I am very new to Java Scripting and I have to create a program that collects the data of Name, Age, Birth Month and Day of users and responds with the Name, Age, Birth Month, Day and Year, and astrological sign of the customer/user. It has to have forms & functions used in the code.

    This is what I have so far:
    Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Age check</title>
    <script type="text/javascript">
    <!--
    function calculateAge(inputFieldId, outputFieldId, alert_18){
    
    var age;
    
    var input = document.getElementById(inputFieldId).value;
    
    // Past date info
    
    var pyear = parseInt(input.substring(6,10));
    var pmonth = parseInt(input.substring(3,5)) - 1;
    var pday = parseInt(input.substring(0,2));
    
    // Today info
    today = new Date();
    year = today.getFullYear() ;
    month = today.getMonth();
    day = today.getDate();
    
    if ( month < pmonth ){
    age = year - pyear - 1;
    }
    else if ( month > pmonth ){
    age = year - pyear;
    }
    else if ( month == pmonth ){
    if ( day < pday ){
    age = year - pyear - 1;
    }
    else if ( day > pday ){
    age = year - pyear;
    }
    else if ( day == pday ){
    age = year - pyear;
    }
    }
    document.getElementById(outputFieldId).value = age;
    
    if(alert_18 == 'true'){
    if(age < 18){
    
    //Customize alert message
    
    alert('Attention: under 18!');
    }
    }
    }
    //-->
    </script>
    
    </head>
    <body>
    
    <p>Please enter your date of birth...</p>
    
    <form>
    
    <input type="text" id="birthdate" size="10" onblur="calculateAge('birthdate','age','true')">
    
    <p style="font-size:8pt">
    
    (allowed formats: DD.MM.YYYY, DD/MM/YYYY, DD.MM.YYYY,etc)</p>
    <p>Your age:
    
    <input type="text" id="age" size="2" disabled="disabled"> </p>
    
    </form>
    
    </body>
    </html>
    Last edited by Kor; 05-11-2011 at 07:37 AM. Reason: wrap the code [code][/code]

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Here's the most difficult part of the problem - calculating the astrological sign:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function calculate(f){
    var zodiac={
    'Capricorn1':{'m':0,'d':20},
    'Aquarius':{'m':1,'d':19},
    'Pisces':{'m':2,'d':20},
    'Aries':{'m':3,'d':20},
    'Taurus':{'m':4,'d':21},
    'Gemini':{'m':5,'d':21},
    'Cancer':{'m':6,'d':22},
    'Leo':{'m':7,'d':22},
    'Virgo':{'m':8,'d':23},
    'Libra':{'m':9,'d':23},
    'Scorpio':{'m':10,'d':22},
    'Saggitarius':{'m':11,'d':21},
    'Capricorn2':{'m':11,'d':31}
    }
    var born=f['birthdate'].value;
    born=born.match(/\//)?born.split('/'):born.split('.');
    var yyyy=Number(born[2]);
    var mm=Number(born[1])-1;
    var dd=Number(born[0]);
    var bornDate=new Date(yyyy,mm,dd), zodiacEnd, astrologicalSign;
    for(z in zodiac){
    zodiacEnd=new Date(yyyy,zodiac[z]['m'],zodiac[z]['d']);
    if(bornDate<=zodiacEnd){astrologicalSign=z;break}
    }
    astrologicalSign.match(/\d/)?astrologicalSign=astrologicalSign.replace(/\d/g,''):null;
    var spanSign=document.getElementById('sign');
    spanSign.innerHTML=astrologicalSign;
    return false
    }
    </script>
    </head>
    <body>
    <form action="" onsubmit="return calculate(this)">
    <input type="text" name="birthdate" onblur="calculate(this.form)">
    <span>(allowed formats: DD.MM.YYYY, DD/MM/YYYY)</span>
    </form>
    <br>
    <br>
    <div>Your astroligical sign: <span id="sign"></span></div>
    </body>
    </html>
    The rest should be easy for you, I guess
    Last edited by Kor; 05-11-2011 at 08:57 AM.

  3. #3
    Join Date
    May 2011
    Posts
    2

    Thanks

    You are quick I wish things could flow for me that easy. Not having a programming background is killing me.

    Thanks again Kor for assistance...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles