Click here to Skip to main content
16,016,736 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//I have used a script to calculate age given date of birth, but its not working properly....Kindly let me know my error...I'm showing my code below//



XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="add_pmember.aspx.cs" Inherits="housing1.add_member" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">

        .auto-style8 {
            width: 100%;
            background-color: #CCFFFF;
        }
        .auto-style9 {
            width: 341px;
            text-align: center;
        }
        .auto-style10 {
            width: 341px;
            height: 26px;
            text-align: center;
        }
        .auto-style11 {
            height: 26px;
        }
        .auto-style12 {
            width: 341px;
            height: 23px;
        }
        .auto-style13 {
            height: 23px;
        }
        .auto-style16 {
            width: 341px;
            text-align: center;
            height: 23px;
        }
        .auto-style21 {
            width: 341px;
            height: 24px;
            text-align: center;
        }
        .auto-style22 {
            height: 24px;
            width: 168px;
        }
        .auto-style23 {
            height: 24px;
        }
        .auto-style24 {
            color: #FF0000;
        }
        .auto-style31 {
            width: 128px;
        }
        .auto-style32 {
            height: 26px;
            width: 128px;
        }
        .auto-style33 {
            width: 128px;
            height: 23px;
        }
        .auto-style34 {
            width: 168px;
        }
        .auto-style35 {
            height: 26px;
            width: 168px;
        }
        .auto-style36 {
            width: 168px;
            height: 23px;
        }
    </style>
    <script type="text/javascript">

        function myFunction() {

            alert("You have exceeded the members entered");


        }




</script>

  <script type="text/javascript" src="scripts/jqyery.js"></script>
<script type="text/javascript" src="scripts/moment.min.js"></script>

<script>
    function setAge(d) {
        var age = moment().diff(d, 'years');
        $('#age').val(age);
    }

    $(function () {
        $('.manual').change(function () {
            setAge(moment($(this).val()));
        });
    });
</script>
</head>
<body style="background-color: #CCFFFF">
    <form id="form2" runat="server">
        <asp:Panel ID="Primary_Panel" runat="server" style="background-color: #CCFFFF">
         <table class="auto-style8" cellpadding ="5px">
            <tr>
                <td class="auto-style21">Flat Number</td>
                <td class="auto-style22">
                    <asp:DropDownList ID="DropDownListFN" runat="server" onchange="FlatAvailability()" >
                    </asp:DropDownList>
                </td>
                <td class="auto-style23">
                    <asp:Label ID="lblStatus" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style9">First_Name</td>
                <td class="auto-style34">
                    <asp:TextBox ID="TextBoxFN" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBoxFN" ErrorMessage="First Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                    <br />
                </td>
            </tr>
            <tr>
                <td class="auto-style9">Middle_Name</td>
                <td class="auto-style34">
                    <asp:TextBox ID="TextBoxMN" runat="server"></asp:TextBox>
                </td>

            </tr>
             <tr>
                 <td class="auto-style9">Last_Name</td>
                 <td class="auto-style34">
                     <asp:TextBox ID="TextBoxLN" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator16" runat="server" ControlToValidate="TextBoxLN" ErrorMessage="Last Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                     <br />
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">Mobile Number</td>
                 <td class="auto-style34">
                     <asp:TextBox ID="TextBoxMO" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBoxMO" ErrorMessage="Mobile Number is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                     <br />
                 </td>
             </tr>
            <tr>
                <td class="auto-style10">Email</td>
                <td class="auto-style35">
                    <asp:TextBox ID="TextBoxE" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style11">
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBoxE" ErrorMessage="Invalid Email" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" SetFocusOnError="True"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style10" class="manual">Date Of Birth</td>
                <td class="auto-style35">
                    <asp:TextBox ID="TextBoxDOB" runat="server"></asp:TextBox>
                </td>

                <td>
                    <span class="auto-style24">(mm/dd/yyyy)</span>
                    <asp:Label ID="lblAge" runat="server"></asp:Label>
                </td>

            </tr>
            <tr>
                <td class="auto-style9">Age</td>
                <td class="auto-style34">
                    <asp:TextBox ID="age" runat="server"></asp:TextBox>
                </td>
                <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="age" ErrorMessage="Age is required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
            </tr>
             <tr>
                 <td class="auto-style16">Educational Qualification</td>
                 <td class="auto-style36">
                     <asp:TextBox ID="TextBoxEQ" runat="server"></asp:TextBox>
                 </td>
                 <td class="auto-style13"></td>
             </tr>
             <tr>
                 <td class="auto-style9">Office Address</td>
                 <td class="auto-style34">
                     <asp:TextBox ID="TextBoxOA" runat="server" TextMode="MultiLine"></asp:TextBox>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">Native Address</td>
                 <td class="auto-style34">
                     <asp:TextBox ID="TextBoxNA" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TextBoxNA" ErrorMessage="Native Address is required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">PAN Card Number</td>
                 <td class="auto-style34">
                     <asp:TextBox ID="TextBoxPCN" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" ControlToValidate="TextBoxPCN" ErrorMessage="PAN Card No Is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">Aadhar Card Number</td>
                 <td class="auto-style34">
                     <asp:TextBox ID="TextBoxACN" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator14" runat="server" ControlToValidate="TextBoxACN" ErrorMessage="Aadhar number is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">Religion</td>
                 <td class="auto-style34">
                     <asp:TextBox ID="TextBoxR" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator15" runat="server" ControlToValidate="TextBoxR" ErrorMessage="Religion is Mandatory" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style16">Business/Job</td>
                 <td class="auto-style36">
                     <asp:RadioButtonList ID="RadioButtonListBJ" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="Business">Business</asp:ListItem>
                         <asp:ListItem Value="Job">Job</asp:ListItem>
                     </asp:RadioButtonList>
                 </td>
                 <td class="auto-style13">
                     &nbsp;</td>
             </tr>
             <tr>
                 <td class="auto-style16">Married/UnMarried</td>
                 <td class="auto-style36">
                     <asp:RadioButtonList ID="RadioButtonListMU" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="Married">Married</asp:ListItem>
                         <asp:ListItem Value="Unmarried">Unmarried</asp:ListItem>
                     </asp:RadioButtonList>
                 </td>
                 <td class="auto-style13">
                     &nbsp;</td>
             </tr>
            <tr>
                <td class="auto-style9">No Of Members</td>
                <td class="auto-style34">
                    <asp:TextBox ID="TextBoxNOM" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="TextBoxNOM" ErrorMessage="Number Of Members is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                    <br />
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ControlToValidate="TextBoxNOM" ErrorMessage="Only Numbers are allowed" ForeColor="Red" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>
                </td>
            </tr>
             <tr>
                 <td class="auto-style9">Joining Date</td>
                 <td class="auto-style34">
                     <asp:TextBox ID="TextBoxJ" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator21" runat="server" ControlToValidate="TextBoxJ" ErrorMessage="Joining Date is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
            <tr>
                <td class="auto-style12" colspan="1">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="text-align: center" Text="ADD" />
               </td>
                <td class="auto-style34">
                    <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Family Member Details" />


                </td>
                <td class="auto-style13"></td>
                <td class="auto-style13"></td>
            </tr>
        </table>
         </asp:Panel>
        <asp:Panel ID="Secondary_Panel" runat="server">
             <table class="auto-style8" cellpadding ="5px">
            <tr>
                <td class="auto-style9">Primary Member Name</td>
                <td class="auto-style31">
                    <asp:TextBox ID="TextBoxPMN" runat="server" ></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="TextBoxPMN" ErrorMessage="Primary Member Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style9">First_Name</td>
                <td class="auto-style31">
                    <asp:TextBox ID="TextBoxFN1" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBoxFN1" ErrorMessage="Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
             <tr>
                <td class="auto-style9">Middle_Name</td>
                <td class="auto-style31">
                    <asp:TextBox ID="TextBoxMN1" runat="server"></asp:TextBox>
                </td>

            </tr>
             <tr>
                 <td class="auto-style9">Last_Name</td>
                 <td class="auto-style31">
                     <asp:TextBox ID="TextBoxLN1" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="TextBoxLN1" ErrorMessage="Last Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">Mobile Number</td>
                 <td class="auto-style31">
                     <asp:TextBox ID="TextBoxMO1" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="TextBoxMO1" ErrorMessage="Mobile Number is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                     <br />
                 </td>
             </tr>
            <tr>
                <td class="auto-style10">Email</td>
                <td class="auto-style32">
                    <asp:TextBox ID="TextBoxE1" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style11">
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBoxE1" ErrorMessage="Invalid Email" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style10">Date Of Birth</td>
                <td class="auto-style32">
                    <asp:TextBox ID="TextBoxDOB1" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style11">
                    <span class="auto-style24">(mm/dd/yyyy)</span></td>

                <td>

                    &nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style9">Age</td>
                <td class="auto-style31">
                    <asp:TextBox ID="TextBoxA1" runat="server"></asp:TextBox>
                </td>
                <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="TextBoxA1" ErrorMessage="Age is required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
            </tr>
             <tr>
                 <td class="auto-style16">Educational Qualification</td>
                 <td class="auto-style33">
                     <asp:TextBox ID="TextBoxEQ1" runat="server"></asp:TextBox>
                 </td>
                 <td class="auto-style13"></td>
             </tr>
             <tr>
                 <td class="auto-style9">Office Address</td>
                 <td class="auto-style31">
                     <asp:TextBox ID="TextBoxOA1" runat="server" TextMode="MultiLine"></asp:TextBox>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">Native Address</td>
                 <td class="auto-style31">
                     <asp:TextBox ID="TextBoxNA1" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" ControlToValidate="TextBoxNA1" ErrorMessage="Native Address is required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">PAN Card Number</td>
                 <td class="auto-style31">
                     <asp:TextBox ID="TextBoxPCN1" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator18" runat="server" ControlToValidate="TextBoxPCN1" ErrorMessage="PAN Card No Is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">Aadhar Card Number</td>
                 <td class="auto-style31">
                     <asp:TextBox ID="TextBoxACN1" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator19" runat="server" ControlToValidate="TextBoxACN1" ErrorMessage="Aadhar number is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style9">Religion</td>
                 <td class="auto-style31">
                     <asp:TextBox ID="TextBoxR1" runat="server"></asp:TextBox>
                 </td>
                 <td>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator20" runat="server" ControlToValidate="TextBoxR1" ErrorMessage="Religion is Mandatory" ForeColor="Red"></asp:RequiredFieldValidator>
                 </td>
             </tr>
             <tr>
                 <td class="auto-style16">Business/Job</td>
                 <td class="auto-style33">
                     <asp:RadioButtonList ID="RadioButtonListBJ1" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="Business">Business</asp:ListItem>
                         <asp:ListItem Value="Job">Job</asp:ListItem>
                     </asp:RadioButtonList>
                 </td>
                 <td class="auto-style13">
                     &nbsp;</td>
             </tr>
             <tr>
                 <td class="auto-style16">Married/UnMarried</td>
                 <td class="auto-style33">
                     <asp:RadioButtonList ID="RadioButtonListMU1" runat="server" RepeatDirection="Horizontal" Width="171px">
                         <asp:ListItem Value="Married">Married</asp:ListItem>
                         <asp:ListItem Value="Unmarried">Unmarried</asp:ListItem>
                     </asp:RadioButtonList>
                 </td>
                 <td class="auto-style13">
                     &nbsp;</td>
             </tr>
            <tr>
                <td class="auto-style12" colspan="2">
                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" style="text-align: center" Text="ADD" />


                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="Button4" runat="server" OnClick="Button2_Click" Text="BACK" />


                </td>
                <td class="auto-style13"></td>
                <td class="auto-style13"></td>
            </tr>
        </table>
        </asp:Panel>
        <p>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HousingConnectionString %>" SelectCommand="SELECT [Flat_No ] AS Flat_No_, [Floor], [Name], [Mobile_NO], [Email], [DOB], [Office_Add], [No_Of_Members] FROM [Primary_Member]"></asp:SqlDataSource>
        </p>
        <p>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/webform1.aspx">Go To Menu</asp:HyperLink>
        </p>


      <script src="../Scripts/jquery-1.7.1.min.js"></script>

    <script type="text/javascript">

        function FlatAvailability() {
            //This function call on text change.
            $.ajax({
                type: "POST",
                url: "add_pmember.aspx/CheckFlat", // this for calling the web method function in cs code.
                data: '{flat: "' + $("#<%=DropDownListFN.ClientID%>")[0].value + '"}',// flat name
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response);
                }
            });
        }

        // function OnSuccess
        function OnSuccess(response) {
            var msg = $("#<%=lblStatus.ClientID%>")[0];
            switch (response.d) {
                case "true":
                    msg.style.display = "block";
                    msg.style.color = "red";
                    msg.innerHTML = "Flat Detail Already  exists.";
                    break;
                case "false":
                    msg.style.display = "block";
                    msg.style.color = "green";
                    msg.innerHTML = "";
                    break;
            }
        }

        </script>


    </form>


</body>
</html>
Posted

1 solution

I guess you've used moment.js. :)
But I would do something like this:
JavaScript
function calculateAge(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;
}


-KR
 
Share this answer
 

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