asp.net中的clientvalidation

时间:2012-03-07 01:54:37

标签: c# asp.net

我正在尝试使用customvalidator创建必需的字段验证。但是当该字段为空时,它仍然会进行回发吗?

<body>
    <form id="Form1" runat="server">
    <h3>
        CustomValidator ServerValidate Example</h3>
    <asp:Label ID="Message" Font-Name="Verdana" Font-Size="10pt" runat="server" />
    <p>
        <asp:TextBox ID="Text1" runat="server" Text="[Name:required]" />
        &nbsp;&nbsp;
        <asp:CustomValidator ID="CustomValidator1" ControlToValidate="Text1" ClientValidationFunction="ClientValidate"
            Display="Static" ErrorMessage="" ForeColor="green" Font-Name="verdana" Font-Size="10pt"
            runat="server" />
        <p>
            <asp:Button ID="Button1" Text="Validate" OnClick="ValidateBtn_OnClick" runat="server" />
    </form>
</body>
</html>
<script language="javascript">
    function ClientValidate(source, arguments) {
        alert(arguments.Value.length);
        if (arguments.Value != "[Name:required]" && arguments.Value.length > 0) {
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
    }
</script>

2 个答案:

答案 0 :(得分:0)

ValidateEmptyText="True"添加到CustomValidator标记

有关详细信息,请参阅here

答案 1 :(得分:0)

您应该将包含ClientValidate功能的<script>代码移至<html>代码中,最好在<body><head>代码内。