2012-03-07 1 views
2

私はcustomvalidatorで必須のフィールド検証を作成しようとしています。しかし、フィールドが空の場合は、まだポストバックが行われますか?asp.netでclientvalidation

<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 =" MyValidationGroup "などの' ValidationGroup'を使用してみてください。また、ボタンに 'CausesValidation = true'属性を付けることもできます。 – Akhil

答えて

0

は、詳細はhereを参照してくださいあなたのCustomValidatorタグにValidateEmptyText="True"を追加します。

0

ClientValidate関数を含む<script>タグは、<html>タグ内、好ましくは<body>タグまたは<head>タグ内に移動する必要があります。

関連する問題