Jump to content

validate date of birth


rockythree

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...