2012-03-07 54 views
2

我正在嘗試使用customvalidator創建必需的字段驗證。然而,當該字段爲空時,它仍然會回傳?asp.net中的客戶端驗證

<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> 
+0

嘗試在文本框和按鈕上使用「ValidationGroup」,如「ValidationGroup =」MyValidationGroup「」。您也可以在按鈕上具有'CausesValidation = true'屬性。 – Akhil 2012-03-07 01:59:45

回答

0

添加ValidateEmptyText="True"到您的CustomValidator標籤

詳情請參閱here

0

您應該將包含ClientValidate功能的<script>標記移動到<html>標記中,最好在<body><head>標記內。