ASP.NET Web Forms Web Forms Server Controls Web Forms Validation
If the user is working with a browser that supports Dynamic HTML ( DHTML ), such as Internet Explorer 4.0 or higher, validation controls can perform validation using client script. Because the controls can provide immediate feedback ( without a round trip to the server ), the user experience with the page is enhanced.
Under most circumstances, you do not have to make any changes to your page or to the validation controls. They detect automatically if the browser supports DHTML and perform their checking accordingly. Client-side validation uses the same error display mechanism as server-side checking.
NOTE: Validation is performed on the server even if it was already performed on the client. This allows you to check validation status in server code and provides security against users bypassing validation.
There are some additional features of validation available if it is performed on the client:
- If you are summarizing validation error messages, you can display them in a pop-up message box that appears when the user submits the page. For details, see Controlling Validation Message Display.
- You can write client script to enhance or replace the default validation and error reporting behavior of the validation controls. For details, see Enhancing or Replacing Client-Side Validation.
- The object model for validation controls is slightly different on the client. See Client Validation Object Model below.
- There are a few minor differences associated with client-side validation:
- If client-side validation is enabled, the page includes references to script libraries that are used to perform the client-side validation.
- When you use a RegularExpressionValidator control, the expressions are checked on the client using the regular expression facility of Jscript.NET. This differs in some very small details from the regular expression checking done on the server using the class.
- The page includes a client-side method to intercept and handle the page’s OnSubmit event.
- create Change event-handling methods for the controls being validated. If you are writing methods in client script for these events, you cannot bind the Change event directly from the input control, because the validation controls will override your binding. Instead, you bind indirectly using the FOR and EVENT attributes of a script block
Validation controls present almost the same object mode on the client as on the server. For example, you can test validation by reading the value of a validation control’s IsValid property the same way on both the client and the server.
However, there are differences in the validation information exposed at the page level. On the server, the page supports properties; on the client, it contains global variables. The following table compares the information exposed on the page.
Server Page Property |
Client Page Variable |
IsValid |
Page_IsValid |
Validators ( collection ) — Contains references to validation controls whose IsValid property has changed since the last round trip to the server. |
Page_Validators ( array ) — Contains references to all validation controls on the page, regardless of change in their status. |
( no equivalent ) |
Page_FieldsToValidate ( array ) — A list of references to all the input controls being validated. This array is used internally by the validation controls. |
( no equivalent ) |
Page_ValidationActive — A boolean value that you can use to turn validation on and off for the page as a whole. This is useful, for example, if you want to disable validation temporarily. |
NOTE: All page-related validation information should be considered read-only.
Client-side validation is enabled automatically if you are using Microsoft Internet Explorer 4.0 or later. In some instances, however, you might prefer server-side validation even if client-side validation is possible. For example, server-side validation might be required if the validation requires information or resources that are only available on the server, such as access to a database. For details, see Controlling Client vs. Server Validation.
NOTE: Client-side validation requires that your browser support Dynamic HTML and Visual Basic.
By default, when client-side validation is being performed, the user cannot post the form to the server if there are errors on the page. However, you might find it necessary to allow the user to post even with errors. For example, if the user is working with a multi-page entry form, an error on the first page might be resolved by an entry on a subsequent page. For details, see Allowing Posting with Client-Side Validation Errors.
Data Entry Validation ( Client-Side )