Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Issue with javascript

    Hey everyone, I've got this slight problem with my code in javascript and I cant solve it to save my life and I don't really want to mess more things around incase it makes it worse.

    For some reason, when I click "get age" it just doesnt run, but the if statements seem to be running, any help would be appreciated

    Code:
    <script type = "text/javascript">
    	
    	function dIM(Y, M) {
        with (new Date(Y, M, 1, 12)) {
            setDate(0);
            return getDate();
    	}
    }
    	function dateDifference(birthdate1, birthdate2)
    	{
    		var year1 = birthdate1.getFullYear(), month1 = birthdate1.getMonth(), day1 = birthdate1.getDate(),
    		year2 = birthdate2.getFullYear(), month2 = birthdate2.getMonth(), day2 = birthdate2.getDate();
    		
    			if (day1 < day2){
    					month1--;
    					day1 += dIM (year2, month2);day
    			};
    				if (month1 < month2) {
    						year1--;
    						month1 += 12;
    					
    					}
    			
    			return [year1 - year2, month1 - month2, day1 - day2];
    		}
    		
    	function ageCalculator() {
    	
    		var day = document.age.inputdate.value;
    		var month = (document.age.inputmonth.value - 1);
    		var year = document.age.inputyear.value;
    		
    		var now = new Date();
    		thisday = now.getDate();
    		thismonth = now.getMonth();
    		thisyear = now.getFullYear();
    		
    		var first = new Date(thisyear, thismonth - 1, thisday);
    		var second = new Date(year, month - 1, day);
    		var yourage;
    		var datediff;
    		
    		if (day == "" || month == "" || year == "")
    		{
    			alert ("Please fill in all of the boxes before getting your age");
    			}
    	
    		else if ((day != parseInt(day)) || (month != parseInt(month)) || (year != parseInt(year)))
    			{
    				alert ("Please only enter digits in the day, month or year boxes"); return false;		
    			}
    		
    		datediff = dateDifference(first,second);	
    
    		if ((thismonth < month) || (thismonth == month & thisday<=day)) {thisyear--;}
    		
    		yourage = thisyear-year;
    		
    		var next = parseInt(year)+datediff[0]+1;
    		var difference = Date.UTC(next, month, day, 0, 0, 0) - Date.UTC(thisyear, thismonth, thisday, 0, 0, 0);
    		var daysleft = difference/1000/60/60/24;
    		
    		document.age.daysremaining.value = daysleft+" days left for your next birthday";
    		document.age.ageoutput.value = yourage;
    			
    	}
    	
    		function clear(form){
    			form.Result.value = "";
    			
    			}
    </script>
        <center>
      <p>
      	<form name = age>
    	<p>Day of birth&nbsp;&nbsp;&nbsp;&nbsp;
    	  <input type="text" id="inputdate" size="2"><br/>
    	  Month of birth&nbsp;
    	  <input type="text" id="inputmonth" size="2"><br/>
    	  Year of birth 
    	  <input type="text" id="inputyear" size="4" ><br/>
          <input name="button" type="button" id="button2" onClick="ageCalculator()" value = "Get Age"/>
          <input type="reset" name="Reset" id="button" value="Reset" />
    	
        </p>
    	<p> You are
      <input type = "text" name = "ageoutput" size = "4" value = "0"> years old
      <input type = "text" name = "daysremaining" value = "0" />
    	  </p>
      	</form>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,722
    Thanks
    25
    Thanked 662 Times in 661 Posts
    This has nothing to do with your question but <form> needs an action, name is deprecated, id is used instead, and the attributes should be in quotes.
    So, <form name = age> S/B <form id="age" action="some action">.

    A trouble shooting tip use alert('here');die; as the first line in a function to see if your getting there. In this case you are. Next trouble shooting tip Check your inputs. So I put alert(day);die; after var day = document.age.inputdate.value;
    I got nothing. If I got an alert box I would know the data was wrong but nothing means the line(code) is wrong. Changed it to var day = document.getElementById('age').inputdate.value; Got the alert.

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the help, I did the corrections but unfortunately I still can't get it work, thanks anyways.

    If anyone else can solve this, I'd be very grateful

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,722
    Thanks
    25
    Thanked 662 Times in 661 Posts
    You should post your new code

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    It works for me in IE9 and correctly calculates my age.

    The with statement is very unreliable, is deprecated and generally discouraged, and may cause problems in other browsers.


    Quizmaster: What word is used for the letter "B" in the phonetic alphabet?
    Contestant: Bob

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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