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 12-08-2011, 09:11 PM   PM User | #1
gatew
New to the CF scene

 
Join Date: Dec 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
gatew is an unknown quantity at this point
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>
gatew is offline   Reply With Quote
Old 12-09-2011, 06:17 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
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.
sunfighter is offline   Reply With Quote
Old 12-09-2011, 08:16 PM   PM User | #3
gatew
New to the CF scene

 
Join Date: Dec 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
gatew is an unknown quantity at this point
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
gatew is offline   Reply With Quote
Old 12-10-2011, 03:36 PM   PM User | #4
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
You should post your new code
sunfighter is offline   Reply With Quote
Old 12-10-2011, 05:59 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is online now   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 10:18 AM.


Advertisement
Log in to turn off these ads.