Compartilhar via


CustomValidator.ClientValidationFunction Propriedade

Definição

Obtém ou define o nome da função de script do lado do cliente personalizada usada para validação.

public:
 property System::String ^ ClientValidationFunction { System::String ^ get(); void set(System::String ^ value); };
public string ClientValidationFunction { get; set; }
[System.Web.UI.Themeable(false)]
public string ClientValidationFunction { get; set; }
member this.ClientValidationFunction : string with get, set
[<System.Web.UI.Themeable(false)>]
member this.ClientValidationFunction : string with get, set
Public Property ClientValidationFunction As String

Valor da propriedade

O nome da função de script de cliente personalizada usada para validação. O valor padrão é Empty, que indica que essa propriedade não está definida. O nome da função não deve incluir nenhum parêntese ou parâmetro.

Atributos

Exemplos

O exemplo de código a seguir demonstra como usar a ClientValidationFunction propriedade para especificar o nome da função que executa a validação do lado do cliente. A função de validação verifica se há números pares. Para obter uma descrição dos parâmetros de função, consulte a seção Comentários de CustomValidator.

Importante

Este exemplo tem uma caixa de texto que aceita a entrada do usuário, que é uma possível ameaça à segurança. Por padrão, ASP.NET páginas da Web validam que a entrada do usuário não inclui elementos html ou script. Para obter mais informações, consulte Visão geral de explorações de script.

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      void ValidateBtn_OnClick(object sender, EventArgs e) 
      { 
         // Display whether the page passed validation.
         if (Page.IsValid) 
         {
            Message.Text = "Page is valid.";
         }

         else 
         {
            Message.Text = "Page is not valid!";
         }
      }

      void ServerValidation(object source, ServerValidateEventArgs args)
      {
         try 
         {
            // Test whether the value entered into the text box is even.
            int i = int.Parse(args.Value);
            args.IsValid = ((i%2) == 0);
         }

         catch(Exception ex)
         {
            args.IsValid = false;
         }
      }
       
   </script>    

</head>
<body>

   <form id="Form1" runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <p>

      <asp:TextBox id="Text1" 
           runat="server" />
    
        

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           ClientValidationFunction="ClientValidate"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Not an even number!"
           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)
   {
        if (arguments.Value % 2 == 0 ){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      Sub ValidateBtn_OnClick(sender As Object, e As EventArgs) 
           ' Display whether the page passed validation.
           If Page.IsValid Then
               Message.Text = "Page is valid."
           Else
               Message.Text = "Page is not valid!"
           End If
       End Sub

      Sub ServerValidation(source As Object, args As ServerValidateEventArgs)
           Try
               ' Test whether the value entered into the text box is even.
               Dim num As Integer = Integer.Parse(args.Value)
               args.IsValid = ((num Mod 2) = 0)
           Catch ex As Exception
               args.IsValid = False
           End Try
       End Sub

   </script>      

</head>
<body>

   <form id="Form1" runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <p>

      <asp:TextBox id="Text1" 
           runat="server" />
    
        

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           ClientValidationFunction="ClientValidate"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Not an even number!"
           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)
   {
        if (arguments.Value % 2 == 0 ){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>

Comentários

Defina essa propriedade como o nome da função que executa a validação do lado do cliente.

Como a função de validação do cliente é executada no navegador de destino, a função deve ser gravada usando uma linguagem de script compatível com o navegador, como JScript ou VBScript.

Essa propriedade não pode ser definida por temas ou temas de folha de estilos. Para obter mais informações, consulte ThemeableAttribute e ASP.NET Temas e Capas.

Aplica-se a

Confira também