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
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. |
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.
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>