NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Validating with a Custom Function

If the existing validation controls do not suit your needs, you can define a custom validation function and call it using special validation control. The control then calls your custom server-side validation logic.

To validate with a custom function

  1. Add a CustomValidator control to the page and set the following properties:
    Property Setting
    ControlToValidate The ID of the control you are validating.
    ErrorMessage, Text, ValidationDisplay Properties that specify the text and location of the error or errors that will display if the user skips the control. For details, see Controlling Validation Error Message Display.
  2. Specify the name of a validation function for the control to call by setting the ServerValidationFunction to the name of a method to call for validation.
  3. Create the validation function, which must have a signature such as the following:
    function Name(validationControl, value)

    The validationControl parameter is a reference to the custom validation control calling this function, and value contains the user input to validate. The function should return true if the value is valid, false otherwise.

    On the client, the function should be written in ECMAScript and placed in a <SCRIPT> block. On the server, you write it in the language that you are using for your Web Form page.

  4. Add a test in your Web Forms code to check for validity. For details, see Testing Validity Programmatically.
    Note   You can also create client-side custom validation. To do so, you follow this procedure but specify a method name for the ClientValidationFunction and create a function in client script that duplicates the logic of the server-side method.

The following example illustrates custom client-side validation. An excerpt from the page shows a Textbox control referenced by a CustomValidator validation control. The validation control calls a function called validateLength to make sure that the user has entered at least 8 characters into the Textbox control.

<SCRIPT LANGUAGE="JavaScript">
function validateLength( oSrc, txtValue ){
   var retValue = true; // assume ok
   if(txtValue.length < 8){
      retValue = false;}
   return retValue
}
</SCRIPT>

<asp:Textbox id="text1" runat="server" text="">
</asp:Textbox>

<asp:CustomValidator id="CustomValidator1" runat=server
   ControlToValidate="text1"
   ErrorMessage="You must enter at least 8 characters! " >
   ClientValidationFunction="validateLength(o, v)"
</asp:CustomValidator>

See Also

  1. Validation Controls | Introduction to Validating User Input in Web Forms |