C# .NET - javascript For Age - Asked By ricky ponting on 09-Feb-12 05:40 AM

Hi
  plz give Age Javascript
dipa ahuja replied to ricky ponting on 09-Feb-12 05:43 AM
<script language="JavaScript" type="text/javascript">

  function checkAge() {
    var Agetxt = document.getElementById("<%= TextBox1.ClientID %>").value;
 
    if (isNaN(Agetxt) || Agetxt < 18 || Agetxt > 60) {
      alert("The age must be a number between 18 and 60");
      return false;
    }     
  }    
</script>
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  
  <asp:Button ID="Button3" runat="server" Text="CheckAGe" OnClientClick="return checkAge();" />
  
 
smr replied to ricky ponting on 09-Feb-12 05:45 AM
hi

here is an example of calculating age using javascript

function getAge(dateString) {
  var today = new Date();
  var birthDate = new Date(dateString);
  var age = today.getFullYear() - birthDate.getFullYear();
  var m = today.getMonth() - birthDate.getMonth();
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age--;
  }
  return age;
}


follow
http://stackoverflow.com/questions/4060004/calculate-age-in-javascript
http://www.aleixcortadellas.com/main/2009/02/28/calculating-age-with-javascript/
Web Star replied to ricky ponting on 09-Feb-12 10:50 AM
There are two different way to calculate age in javascript
see in this link
http://pankajlalwani.wordpress.com/2010/08/02/age-calculation-from-date-of-birth-using-javascriptjquery/