Saturday, 2 November 2013

age must be greaterthan 18 based on dtaeofbirth condition checking in javascript












cheking age in javascript


 // This fuction is used for age checking

    function checkage() {
        var lblage = document.getElementById('<%=lblage.ClientID %>');
        lblage.innerHTML = '';
        var todaydate = $get('<%= hftodaydate.ClientID %>').value;
        var birthdate = document.getElementById('<%=txtdob.ClientID %>');
        if (todaydate != '' && birthdate != '') {
            var d1 = todaydate.split("/");
            var d2 = birthdate.value.split("/");
            var tdate = new Date(d1[2], d1[1] - 1, d1[0]);
            var bdate = new Date(d2[2], d2[1] - 1, d2[0]);
            if (bdate < tdate) {
                var currentyear = tdate.getFullYear();
                var birthyear = bdate.getFullYear();
                var age = parseInt(currentyear) - parseInt(birthyear);
                if (parseInt(age) < 18) {
                    lblage.innerHTML = 'Age Is Must be Greaterthan 18';
                    birthdate.value = '';
                    birthdate.focus();
                }
            }
            else {
                lblage.innerHTML = 'Age Is Must be Greaterthan 18';
            }
        }
        else {
            lblage.innerHTML = 'Enter DateOfBirth';
        }
    }


your asp.x code

<asp:TextBox ID="txtdob" MaxLength="15" runat="server" CssClass="reform-input" onchange="checkage();"></asp:TextBox>
            <ajaxToolkit:FilteredTextBoxExtender ID="FTBEtxtactivationdate" runat="server" TargetControlID="txtdob"
                FilterType="Custom" ValidChars="0123456789/" FilterMode="ValidChars">
            </ajaxToolkit:FilteredTextBoxExtender>
            <ajaxToolkit:CalendarExtender ID="CEtxtactivationdate" runat="server" TargetControlID="txtdob"
                BehaviorID="CEtxtactivationdate" Format="dd/MM/yyyy">
            </ajaxToolkit:CalendarExtender>
            <asp:RequiredFieldValidator ID="RFVtxtdob" runat="server" ErrorMessage="Enter DOB"
                SetFocusOnError="true" Display="None" ValidationGroup="account" ControlToValidate="txtdob"></asp:RequiredFieldValidator>
            <ajaxToolkit:ValidatorCalloutExtender ID="VCEtxtdob" runat="server" TargetControlID="RFVtxtdob">
            </ajaxToolkit:ValidatorCalloutExtender>
            <asp:RegularExpressionValidator ID="rgeDob" runat="server" ControlToValidate="txtdob"
                SetFocusOnError="True" ErrorMessage="Please enter dob in dd/mm/yyyy format" ValidationExpression="^(((0[1-9]|[12]\d|3[01])/(0[13578]|1[02])/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)/(0[13456789]|1[012])/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])/02/((19|[2-9]\d)\d{2}))|(29/02/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"
                Display="None"></asp:RegularExpressionValidator>
            <ajaxToolkit:ValidatorCalloutExtender ID="VCE1_txtdob" runat="server" TargetControlID="rgeDob">
            </ajaxToolkit:ValidatorCalloutExtender>
            <asp:Label ID="lblage" runat="server" ForeColor="Red"></asp:Label>

No comments:

Post a Comment