rockythree Posted April 25, 2012 Share Posted April 25, 2012 hello everyone i have a question in javascript, I've created a function in javascript that you give it your date of birth dd/mm/yyyy then the function calculate your agethe problem is that the function accept any number you give for example it will accept like this date of birth( 44/44/1988) even though there is nodate or month 44 it will still count it. if anyone can resolve the problem appreciate it. Link to comment Share on other sites More sharing options...
rockythree Posted April 25, 2012 Author Share Posted April 25, 2012 here is the code: <script language="javascript"><!--function Age(){ var bday=parseInt(document.forms[0].txtBday.value); var bmo=(parseInt(document.forms[0].txtBmo.value)-1); var byr=parseInt(document.forms[0].txtByr.value); var byr; var age; var now = new Date(); tday=now.getDate(); tmo=(now.getMonth()); tyr=(now.getFullYear());{ if((tmo > bmo)||(tmo==bmo & tday>=bday)) {age=byr} else {age=byr+1}//alert("As of today, "+now+' \n'+", you are:"+(tyr-age)+ " years old");document.getElementById('age1').value = (tyr-age);if (bday > 32){document.write("date should be less than 32");}}}//--> </script> Link to comment Share on other sites More sharing options...
rockythree Posted April 25, 2012 Author Share Posted April 25, 2012 25 views with no replys Link to comment Share on other sites More sharing options...
dsonesuk Posted April 25, 2012 Share Posted April 25, 2012 you will have to validate the month, and then the days in the month, if invalid throw up an error alert or message, and not fully process until these pass validation. Try an array with total days for each month, use month value as the index for retrieving total days from the array, and then check day value is greater than 0 and less than or equal to the total days value in array. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.