CustomValidator.ClientValidationFunction Property
Gets or sets the name of the custom client-side script function used for validation.
[Visual Basic] Public Property ClientValidationFunction As String [C#] public string ClientValidationFunction {get; set;} [C++] public: __property String* get_ClientValidationFunction(); public: __property void set_ClientValidationFunction(String*); [JScript] public function get ClientValidationFunction() : String; public function set ClientValidationFunction(String);
Property Value
The name of the custom client script function used for validation. The default value is String.Empty, which indicates that this property is not set.
Note The function name should not include any parentheses or parameters.
Remarks
Set this property to the name of the function that performs the client-side validation.
Because the client validation function runs on the target browser, the function must be written using a scripting language supported by the browser, such as JScript or VBScript.
Example
[Visual Basic, C#] The following example demonstrates how to use the ClientValidationFunction property to specify the name of the function that performs client-side validation. The validation function checks for even numbers.
[Visual Basic] <%@ 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 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="vbscript"> <!-- Sub ClientValidate(source, arguments) If (arguments.Value mod 2) = 0 Then arguments.IsValid=true Else arguments.IsValid=false End If End Sub ' --> </script> [C#] <%@ 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 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="vbscript"> <!-- Sub ClientValidate(source, arguments) If (arguments.Value mod 2) = 0 Then arguments.IsValid=true Else arguments.IsValid=false End If End Sub ' --> </script>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
CustomValidator Class | CustomValidator Members | System.Web.UI.WebControls Namespace | String.Empty