ASP.NET Web Forms Web Forms Server Controls Web Forms Validation
If the existing validation controls do not suit your needs, you can define a custom server-side validation function and call it using the CustomValidator control. You can also add client-side validation for a quick check before the form is submitted, by writing a function in ECMA-compliant JScript or JavaScript that duplicates the logic of the server-side method.
NOTE: You should always perform server-side validation even if you do a client-side check. Server-side validation helps prevent users from bypassing validation by disabling or changing the client script.
Basically, to set up custom server-side validation, you need to
- add a CustomValidator control on the page,
- define the method for checking the user’s input, and
- assign this method to handle the control’s ServerValidate event.
Let’s take a closer look at how all these are done.
- Add a CustomValidator control to the page and set the following properties:
Property |
Setting |
ControlToValidate |
The ID of the input control that the validation control will evaluate. |
Display |
The display behavior for the specified validation control. |
ErrorMessage |
The error message to display in the ValidationSummary control if validation fails. |
Text |
The error message to display in the validation control when validation fails. |
- Create the method to handle the control’s ServerValidate event. This method is called to perform the actual validation when the form is posted. The method must have a signature similar to the following:
void validationMethodName ( object source, ServerValidateEventArgs arg ) {
...
}
Sub validationMethodName ( ByVal source As Object, ByVal arg As ServerValidateEventArgs )
...
End Sub |
|
C# |
VB |
The first parameter ( herein named source ) is a reference to the CustomValidator control that raises the event. The second parameter ( herein named arg ) provides a reference to the ServerValidateEventArgs object, which stores data related to the event.
In particular, this arg object includes:
- a Value property that provides access to the content of the control to validate, and
- an IsValid property that is used to store the result of the validation, which the handler must set to true if the input value is valid, and false otherwise.
The following example shows a simple custom validation handler. The method checks whether the user’s entry is at least eight characters long, and sets the IsValid property accordingly.
void TextValidate ( object source, ServerValidateEventArgs args ) {
args.IsValid = args.Value.Length >= 8;
}
Sub TextValidate ( ByVal source As Object, ByVal args As ServerValidateEventArgs )
args.IsValid = args.Value.Length >= 8
End Sub |
|
C# |
VB |
- Attach the handler to the event by assigning the method name to the onServerValidate attribute of the custom validator control. At runtime, the method is invoked whenever the form is posted to the server.
<asp:textbox id="myTextBox" runat="server" />
<asp:customvalidator runat="server"
onServerValidate="TextValidate"
controltovalidate="myTextBox"
errormessage="Username cannot be less than eight characters." />
- In your Web Forms code, check for validity. For details, see Testing Validity Programmatically.
You can also create client-side custom validation. To do so, you specify a function name for the control’s ClientValidationFunction property, and create a function in client script that duplicates the logic of the server-side method.
- Create a validation function on the client-side in ECMAScript ( ECMA-compliant JavaScript or JScript ) .
The following example illustrates custom client-side validation. An excerpt from the page shows a Textbox control referenced by a CustomValidator control. The validation control calls a function called validateLength to make sure that the user has entered at least eight characters into the Textbox control.
<script language="JavaScript">
function validateLength ( src, arg ) {
args.IsValid = args.Value.length >= 8;
}
</script>
<asp:textbox id="myTextBox" runat="server" text="" />
<asp:customvalidator id="CustomValidator1" runat=server
controltovalidate = "myTextBox"
errormessage = "Username cannot be less than eight characters"
clientvalidationfunction="validateLength" />
- In your Web Forms code, check for validity. For details, see Testing Validity Programmatically.
Show me
Types of Validation CustomValidator Control CustomValidator Class Data Entry Validation ( Client-Side )