使用OnServerValidate和ClientValidationFunction进行CustomValidation

时间:2014-03-18 13:30:41

标签: c# javascript asp.net customvalidator

我在使用CustomValidation控件时遇到问题,我想检查在出生日期文本字段中输入的年份是否小于从下拉列表中选择的毕业年份20年。我认为使用ClientValidationFunction时我尝试使用它会好得多:

<asp:CustomValidator ID="BirthYearCustomValidator" runat="server" ControlToValidate="ddlGraduationYear" ErrorMessage="Enter a valid graduation year." SetFocusOnError="true" ValidationGroup="SaveEducationStep" Display="Dynamic" ClientValidationFunction="GraduationYearValidation"></asp:CustomValidator>

这是脚本

<script type="text/javascript">

function GraduationYearValidation(sender, args) {

    var brithYear = parseInt(new Date(document.getElementById('<%=txtBirthDate.ClientID%>').value).getFullYear());

    var gradeYear = parseInt(document.getElementById('<%=ddlGraduationYear.ClientID%>').options[document.getElementById('<%=ddlGraduationYear.ClientID%>').selectedIndex].text);

    if ((brithYear - gradeYear) < 20) {
        return args.IsValid = true;
    }
    else {
        return args.IsValid = false;
    }
}

我得到了这些错误:document.getElementById(...)为null,并且未定义GraduationYearValidation。

所以,我试图通过以下方式使其成为服务器端:

<asp:CustomValidator ID="BirthYearCustomValidator" runat="server" ControlToValidate="ddlGraduationYear" ErrorMessage="enter a valid graduation year." SetFocusOnError="true" ValidationGroup="SaveEducationStep" Display="Dynamic" OnServerValidate="BirthYearCustomValidator_ServerValidate"></asp:CustomValidator>
背后的代码是:

protected void BirthYearCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int brithYear = Convert.ToDateTime(txtBirthDate.Text).Year;
        int gradeYear = Convert.ToInt32(ddlGraduationYear.SelectedValue);

        if ((gradeYear - brithYear) < 20)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

它不起作用,我搜索的原因我发现它可能是因为我需要写Page.Validate("SaveEducationStep");并在保存前检查是否Page.IsValid,但它仍然无法与我合作 任何关于这两种情况的建议都将受到赞赏。感谢。

1 个答案:

答案 0 :(得分:0)

客户端验证工作正常,请检查您的代码

 <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
    <asp:DropDownList ID="dddate" runat="server">
        <asp:ListItem Text="2014" Value="2014"></asp:ListItem>
        <asp:ListItem Text="2013" Value="2013"></asp:ListItem>
        <asp:ListItem Text="2012" Value="2012"></asp:ListItem>
        <asp:ListItem Text="2011" Value="2011"></asp:ListItem>
    </asp:DropDownList>
    <asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" EnableClientScript="true"             runat="server" ErrorMessage="CustomValidator" ValidationGroup="test" ClientValidationFunction="abc" ControlToValidate="dddate"></asp:CustomValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" ValidationGroup="test" />

 <script type="text/javascript">
    function abc(sender, args) {


        var birthYear = parseInt(new Date(document.getElementById('<%=txtDate.ClientID%>').value).getFullYear());

            var gradeYear = parseInt(document.getElementById('<%=dddate.ClientID%>').options[document.getElementById('<%=dddate.ClientID%>').selectedIndex].text);
            if ((gradeYear - birthYear) > 20) {
                return args.IsValid = true;
            }
            else {
                return args.IsValid = false;
            }
        }


</script>