home *** CD-ROM | disk | FTP | other *** search
Wrap
<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%> <!-- #include virtual="/quickstart/aspplus/include/header.inc" --> <h4>Server Control Form Validation</h4> <p> <div class="indent" style="font-family:Verdana; font-size:8pt;"> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#intro">Introduction to Validation</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#types">Types of Validation Controls</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#client">Client-Side Validation</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#errors">Displaying Validation Errors</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#compare">Working with CompareValidator</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#range">Working with RangeValidator</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#regex">Working with Regular Expressions</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#custom">Performing Custom Validation</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#together">Bringing It All Together</a><br> <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b> <a class="toc2" target="content" href="#endofsection">Section Summary</a><br> </div> <p> <hr> <!--BEGIN SECTION--> <a name="intro"> <span class="subhead">Introduction to Validation</span> <p> The Web Forms framework provides a set of validation server controls that provide an easy-to-use but powerful way to check input forms for errors, and if necessary, display messages to the user. <p> Validation controls are added to a Web Form like other server controls. There are controls for specific types of validation, such as range checking or pattern matching, plus a RequiredFieldValidator that ensures a user does not skip an entry field. You can attach more than one validation control to an input control. For example, you might specify that an entry is both required and that it contain a specific range of values. <p> Validation controls work with a limited subset of HTML and Web server controls. For each control, a specific property contains the value to be validated. Here are the input controls that may be validated: <p> <div class="indent"> <table class="table2" width="400" cellpadding="3"> <tr> <th>Control</th> <th>Validation Property</th> </tr> <tr> <td><b>HtmlInputText</b></td> <td>Value</td> </tr> <tr> <td><b>HtmlTextArea</b></td> <td>Value</td> </tr> <tr> <td><b>HtmlSelect</b></td> <td>Value</td> </tr> <tr> <td><b>HtmlInputFile</b></td> <td>Value</td> </tr> <tr> <td><b>TextBox</b></td> <td>Text</td> </tr> <tr> <td><b>ListBox</b></td> <td>SelectedItem</td> </tr> <tr> <td><b>DropDownList</b></td> <td>SelectedItem</td> </tr> <tr> <td><b>RadioButtonList</b></td> <td>SelectedItem</td> </tr> </table> </div> <!--BEGIN SECTION--> <br> <a name="types"> <br> <span class="subhead">Types of Validation Controls</span> <p> The simplest form of validation is a <b>required field</b>. If the user inputs any value into a field, it is valid. If all of the fields in the page are valid, the page is valid. This example illustrates using the RequiredFieldValidator. <p> <!-- server side required field --> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator1.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator1.src" Icon="/quickstart/aspplus/images/validator1.gif" Caption="Validator1.aspx" runat="server" /> <p> There are also validation controls for specific types of validation, such as range checking or pattern matching. This is the complete list: <p> <div class="indent"> <table class="table2" width="80%" cellpadding=3> <tr> <th>Control Name</th> <th>Description</th> </tr> <tr> <td style="width:25%"><b>RequiredFieldValidator</b></td> <td>Ensures that the user does not skip an entry.</td> </tr> <tr> <td style="width:25%"><b>CompareValidator</b></td> <td>Compares a user's entry against a constant value or a property value of another control using a comparison operator (less than, equal, greater than, and so on).</td> </tr> <tr> <td style="width:25%"><b>RangeValidator</b></td> <td>Checks that a user's entry is between specified lower and upper boundaries. You can check ranges within pairs of numbers, alphabetic characters, and dates. Boundaries can be expressed as constants or as values derived from another control.</td> </tr> <tr> <td style="width:25%"><b>RegularExpressionValidator</b></td> <td>Checks that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on.</td> </tr> <tr> <td style="width:25%"><b>CustomValidator</b></td> <td>Checks the user's entry using validation logic that you code yourself. This type of validation allows you to check for values derived at run time.</td> </tr> <tr> <td style="width:25%"><b>ValidationSummary</b></td> <td>Displays the validation errors in summary form for all of the validators on a page.</td> </tr> </table> </div> <!--BEGIN SECTION--> <br> <a name="client"> <br> <span class="subhead">Client-Side Validation</span> <p> The validation controls always perform validation checking in server code. However, if the user is working with a browser that supports DHTML, the validation controls can also perform validation using client script. With client-side validation, errors are detected immediately and error messages are displayed as soon as the form field containing the error loses focus. This permits the user to correct his input prior to submitting the form to the server. <p> Note that the Web Forms framework always performs validation on the server, even if the validation has already been performed on the client. This helps prevent users from being able to bypass validation by impersonating another user or a pre-approved transaction. <p> Client-side validation is enabled by default. If the client is capable, then uplevel validation will be performed automatically. To disable client-side validation, set the page's ClientTarget property to "Downlevel" ("Uplevel" forces client-side validation). <p> <!-- client-side required field --> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator2.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator2.src" Icon="/quickstart/aspplus/images/validator2.gif" Caption="Validator2.aspx" runat="server" /> <!--BEGIN SECTION--> <br> <a name="errors"> <br> <span class="subhead">Displaying Validation Errors</span> <p> When the user's input is processed (for example, when the form is submitted), the Web Forms framework passes the user's entry to the associated validation control or controls. The validation controls test the user's input and set a property to indicate whether the entry passed the validation test. After all validation controls have been processed, the <b>IsValid</b> property on the page is set; if any of the controls shows that a validation check failed, the entire page is set to invalid. <p> <p> If a validation control is in error, an error message may be displayed in the page by the validation control or in a ValidationSummary control elsewhere on the page. The ValidationSummary control is displayed when the IsValid property of the page is false. It "polls" each of the validation controls on the page and aggregates the text messages exposed by each. This example illustrates displaying errors with a ValidationSummary. <p> <!--ValidationSummary--> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator3.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator3.src" Icon="/quickstart/aspplus/images/validator3.gif" Caption="Validator3.aspx" runat="server" /> <!--BEGIN SECTION--> <br> <a name="compare"> <br> <span class="subhead">Working with CompareValidator</span> <p> The CompareValidator server control compares the values of two controls. CompareValidator uses three key properties to perform its validation. <b>ControlToValidate</b> and <b>ControlToCompare</b> contain the values to compare. <b>Operator</b> defines the type of comparison to perform, for example, Equal or Not Equal. CompareValidator performs the validation by evaluating these properties as an expression: <xmp> ( ControlToValidate <Operator> ControlToCompare ) </xmp> If the expression evaluates true, the validation result is valid. <p> This sample illustrates using the CompareValidator control. <p> <!--CompareVal--> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator4.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator4.src" Icon="/quickstart/aspplus/images/validator4.gif" Caption="Validator4.aspx" runat="server" /> <!--BEGIN SECTION--> <br> <a name="range"> <br> <span class="subhead">Working with RangeValidator</span> <p> The RangeValidator server control tests whether an input value falls within a given range. RangeValidator uses three key properties to perform its validation. <b>ControlToValidate</b> contains the value to validate. <b>MinimumControl</b> and <b>MaximumControl</b> define the minimum and maximum values of the valid range. <p> This sample illustrates using the RangeValidator control. <p> <!-- RangeVal --> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator5.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator5.src" Icon="/quickstart/aspplus/images/validator5.gif" Caption="Validator5.aspx" runat="server" /> <!--BEGIN SECTION--> <br> <a name="regex"> <br> <span class="subhead">Working with Regular Expressions</span> <p> The RegularExpressionValidator server control checks that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on. <p> RegularExpressionValidator uses two key properties to perform its validation. <b>ControlToValidate</b> contains the value to validate. <b>ValidationExpression</b> contains the regular expression to match. <p> These samples illustrates using the RegularExpressionValidator control. <p> <!-- regex --> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator6.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator6.src" Icon="/quickstart/aspplus/images/validator6.gif" Caption="Validator6.aspx" runat="server" /> <p> <!-- more regex--> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator7.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator7.src" Icon="/quickstart/aspplus/images/validator7.gif" Caption="Validator7.aspx" runat="server" /> <!--BEGIN SECTION--> <br> <a name="custom"> <br> <span class="subhead">Performing Custom Validation</span> <p> The CustomValidator server control calls a user-defined function to perform validations that the standard validators can't handle. The custom function can execute on the server or in client-side script, such as JScript or VBScript. For client-side custom validation, the name of the custom function must be identified in the <b>ClientValidationFunction</b> property. The custom function must have the form: <xmp> function myvalidator(source, value)</xmp> Note that <b>source</b> is the client-side CustomValidator object, and <b>value</b> is the value to be validated. <p> For server-side custom validation, place your custom validation in the validator's <b>OnServerValidationFunction</b> delegate. <p> These samples illustrate using the CustomValidator control. <p> <!-- custom --> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator8.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator8.src" Icon="/quickstart/aspplus/images/validator8.gif" Caption="Validator8.aspx" runat="server" /> <!--BEGIN SECTION--> <br> <a name="together"> <br> <span class="subhead">Bringing It All Together</span> <p> This sample shows a typical registration form, using the variations of validation controls we've discussed so far. <p> <!-- summary --> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/webforms/validate/validator9.aspx" ViewSource="/quickstart/aspplus/samples/webforms/validate/validator9.src" Icon="/quickstart/aspplus/images/validator9.gif" Caption="Validator9.aspx" runat="server" /> <!--BEGIN SECTION--> <a name="endofsection"> <h4>Section Summary</h4> <ol> <li>Validator controls can be used to validate input on any Web Form. <li>More than one control can be used on a given input field. <li>Client-side validation may be used in addition to server validation to improve form usability. <li>The CustomValidator control lets the user define custom validation criteria. </ol> <p> <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->