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 a Required Entry

You can specify that a user must provide information in a specific control.

Tip   Required-entry validation is frequently used in conjunction with other types of validation. You can use as many validation controls for a user-entry field as needed.

To validate a required entry

  1. Add a RequiredFieldValidator control to the page and set the following properties:
    Property Setting
    ControlToValidate The ID of the control for which the user must provide a value.
    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. Add a test in your Web Forms code to check for validity. For details, see Testing Validity Programmatically.

If validation is being performed on the client, the user can leave a required field blank while working in the page, but must fill it in before submitting. However, once a value has been filled into the field, the user cannot clear the field. If the field is cleared, the user sees an error message immediately when leaving the field. In server-side validation, no checking is done until the form is submitted, so the user will not see an error message until after the form is submitted.

The following shows an example in the .aspx file of a Web Form Textbox control with required field validation. A table is used to control layout.

<TABLE>
<TR>
<TD>
   <asp:Textbox id="txtLastName" runat="server"></asp:Textbox>
</TD>
<TD>
   <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
      ControlToValidate="txtLastName"
      ErrorMessage="Last name is a required field"
      ForeColor="Red">
   </asp:RequiredFieldValidator>
</TD>
</TR>
</TABLE>

See Also

Validation Controls | Introduction to Validating User Input in Web Forms | Controlling Validation Error Message Display | Controlling Client versus Server Validation