带下拉列表的客户端验证

时间:2013-10-08 09:38:41

标签: javascript asp.net

我有4次下线:开始月份,开始年份,结束月份和结束年份。 在我的javascript中,我需要处理以下验证。

 Start Month should not be greater than End Month
 Start Year should not be greater than End Year
 Month and Year cannot be same (i.e., Oct 2013 - Oct 2013)

我的验证功能如下。

var pStartMonth, pStartYear, pEndMonth, pEndYear;

    pStartMonth = $('#<%= cboMonth1.ClientID %>').val();
    pStartYear = $('#<%= cboYear1.ClientID%>').val();

    pEndMonth = $('#<%= cboMonth2.ClientID%>').val();
    pEndYear = $('#<%= cboYear2.ClientID%>').val();

    function ValidateMonth() {
        if ((pStartMonth > pEndMonth) && (pStartYear = pEndYear)) {
        }
        }

        function ValidateMonthAndYear() {
            if ((pStartMonth = pEndMonth) && (pStartYear = pEndYear)) {

            }
        }

        function ValidateYear(){
            if((pStartMonth = pEndMonth) && (pStartYear > pEndYear))
            {

            }
        }

我在“结束年度”下拉列表中使用了这3个功能:

<asp:DropDownList ID="cboYear2" runat="server" AutoPostBack="true" onclick="javascript:shouldsubmit=false;" >
                                <asp:ListItem Value="0">-Select-</asp:ListItem>
                                <asp:ListItem Value="2013">2013</asp:ListItem>
                                <asp:ListItem Value="2014">2014</asp:ListItem>
                                <asp:ListItem Value="2015">2015</asp:ListItem>
                                <asp:ListItem Value="2016">2016</asp:ListItem>
                                <asp:ListItem Value="2017">2017</asp:ListItem>
                                <asp:ListItem Value="2018">2018</asp:ListItem>
                                <asp:ListItem Value="2019">2019</asp:ListItem>
                                <asp:ListItem Value="2020">2020</asp:ListItem>
                                <asp:ListItem Value="2021">2021</asp:ListItem>
                                <asp:ListItem Value="2022">2022</asp:ListItem>
                                <asp:ListItem Value="2023">2023</asp:ListItem>
                                <asp:ListItem Value="2024">2024</asp:ListItem>
                            </asp:DropDownList>
                            <font color="red">*</font>

                            <asp:RequiredFieldValidator ID="cboYear2_RequiredFieldValidator" runat="server"
                                ErrorMessage="End Year Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboYear2" ValidationGroup="vTimeSlot" Display="None"></asp:RequiredFieldValidator>

                            <asp:CustomValidator ID="CustomValidator1" ControlToValidate="cboYear2" ClientValidationFunction="ValidateMonth"
                                Display="None" ErrorMessage="Prefered Start Month should not be ahead of Prefered End Month" runat="server" ValidationGroup="vTimeSlot"/>

                            <asp:CustomValidator ID="CustomValidator2" ControlToValidate="cboYear2" ClientValidationFunction="ValidateMonthYear"
                                Display="None" ErrorMessage="Prefered Start Month and Prefered End Month and Prefered Start Year and Prefered End Year cannot be the same" runat="server" ValidationGroup="vTimeSlot"/>

                            <asp:CustomValidator ID="CustomValidator3" ControlToValidate="cboYear2" ClientValidationFunction="ValidateYear"
                                Display="None" ErrorMessage="Prefered Start Year should not be ahead of Prefered End Year" runat="server" ValidationGroup="vTimeSlot"/>

但我的所有功能似乎都没有。我错过了什么?

0 个答案:

没有答案