Click here to Skip to main content
15,067,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I calculate age using javascript in asp.net c#.

I have two textboxes. In one textbox I entered date, and on the onblur even, I alled javascript code to calculate age. I could easily show the age in popup. My question is how do I automatically insert age in to the second text box.

My Javascript code:-
JavaScript
<script type="text/javascript">
        function checkAge() {
            var today = new Date();
            var d;
            var dateEntered = document.getElementById('<%=TextBox7.ClientID %>').value;
         var objRegExp = /\d{1,2}\-\d{1,2}\-\d{4}$/
         if (!objRegExp.test(dateEntered)) {
             alert('Date not in correct format');
             return false;
         }
         d = dateEntered.split('-');
         var yearEntered = parseInt(d[2]);
         var nowYear = parseInt(today.getFullYear());
         var age = nowYear - yearEntered;
         alert(age);
         //return false;
     }
    </script>


Aspx code for first textbox:-
ASP.NET
<asp:TextBox ID="TextBox7" onblur="Javascript:checkAge();" runat="server" OnTextChanged="TextBox7_TextChanged" Width="150px"></asp:TextBox>
Posted

Try this[^] out.
   
Comments
Gee Varghese 26-Oct-13 10:11am
   
Thanks dear, but, how can I insert the age to my second textbox ?
If you already calculated age on the Textbox onblur event then you wont have problem to just assign it to the second textbox. I have answered a similar question , May Check the below link

How to Auto Calculate on Page Load[^]

Hope this helps
   
have you tried update panel put your text boxes in update panel
write code in txtboxchangeevent on serverside

C#
 protected void TextBox7_TextChanged(object sender,EventArgs e)
{
// you can check for null before assigning (optional)
yourtextbox.text=TextBox7.Text;

}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900