Get selected date from raddatepicker in javascript

Developer picture Developer · Jun 25, 2012 · Viewed 21.4k times · Source

Hi all my design for Rad Panel bar is as follows

 <telerik:RadPanelBar runat="server" ID="rdpnlPersonal" ExpandMode="FullExpandedItem"
                Skin="Vista" EnableEmbeddedSkins="false" Width="880px">
                <Items>
                    <telerik:RadPanelItem Expanded="true" Text="Employee Personal Information" runat="server"
                        Selected="true">
                        <Items>
                            <telerik:RadPanelItem Value="AccountInformation" runat="server">
                                <ItemTemplate>
                                    <label>
                                        Demop
                                    </label>
                                    <telerik:RadDatePicker ID="RadDatePicker1" Width="200px" runat="server" MinDate="1900-01-01"
                                        TabIndex="5">
                                        <Calendar ID="Calendar1" RangeMinDate="1900-01-01" runat="server">
                                        </Calendar>
                                    </telerik:RadDatePicker>
                                    <telerik:RadDatePicker ID="radDtHiredate" Width="200px" runat="server" MinDate="1900-01-01"
                                        TabIndex="5">
                                        <Calendar ID="calHire" RangeMinDate="1900-01-01" runat="server">
                                        </Calendar>
                                    </telerik:RadDatePicker>
                                    <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validatehiredate"
                                        SetFocusOnError="true" ControlToValidate="radDtHiredate" ValidationGroup="accountValidation"
                                        Display="Dynamic">Can not Hire Guy less than 18 yrs</asp:CustomValidator>
                                    <br />
                                    <telerik:RadButton ID="rdbtnwhAnother" Text="Add WithHolding" runat="server" ButtonType="LinkButton"
                                        ValidationGroup="accountValidation" OnClick="rdbtnwhAnother_Click">
                                    </telerik:RadButton>
                                </ItemTemplate>
                            </telerik:RadPanelItem>
                        </Items>
                    </telerik:RadPanelItem>
                </Items>
            </telerik:RadPanelBar>

Here what I need is if the selected date is less than 18 years I would like to show the error message as per in the Custom validator. For this I have written the following script but I am unable to achieve the required..

My script is as follows

 <script type="text/javascript" language="javascript">
    function monthDiff(d1, d2) {
        alert("Mon");
        var months;
        var date1 = new Date(d1);
        var date2 = new Date(d2);
        return (date1 - date2) / (1000 * 60 * 60 * 24);
        return months;
    }


    //function getLeapYear

    function difference(d1, d2) {
        var hiredate, dob; var diff = 18 * 12;
        hiredate = document.getElementById(d1).get_selectedDate();
        dob = document.getElementById(d2).get_selectedDate();
        var Age = monthDiff(hiredate, dob);

        var compareVal = 365 * 18; //getCompareVal(hiredate,dob);

        if (Age >= compareVal) {
            return true;
            //true
        } else {
            return false; //false
        }
    }
    function validatehiredate(value, arg) {
        var datePicker = $find('<%# rdpnlPersonal.FindItemByText("Employee Personal Information").FindControl("RadDatePicker1").ClientID %>');
        var datePicker1 = $find('<%# rdpnlPersonal.FindItemByText("Employee Personal Information").FindControl("radDtDOB").ClientID %>');
        arg.IsValid = (difference(datePicker, datePicker1));
    }
</script>

Can some one help me please

Answer

Developer picture Developer · Jun 26, 2012

After all I solved as follows by writing the script in radcodeblock as follows

<telerik:RadCodeBlock runat="server" ID="radcb">
        <script type="text/javascript" language="javascript">
            function monthDiff(d1, d2) {
                var months;
                var date1 = new Date(d1);
                var date2 = new Date(d2);
                return (date1 - date2) / (1000 * 60 * 60 * 24);
                return months;
            }

            //function getLeapYear

            function difference(d1, d2) {
                var diff = 18 * 12;
                var Age = monthDiff(d1, d2);

                var compareVal = 365 * 18; //getCompareVal(hiredate,dob);

                if (Age >= compareVal) {
                    return true;
                    //true
                } else {
                    return false; //false
                }
            }
            function validatehiredate(value, arg) {
                var datePicker = $find('<%= rdpnlPersonal.FindItemByValue("AccountInformation").FindControl("radDtHiredate").ClientID %>');
                var datePicker1 = $find('<%= rdpnlPersonal.FindItemByValue("AccountInformation").FindControl("radDtDOB").ClientID %>');

                var date = datePicker.get_selectedDate();
                var date1 = datePicker1.get_selectedDate();
                arg.IsValid = (difference(date, date1));
            }
        </script>
    </telerik:RadCodeBlock>