ASP.NET Form Validation how -to

If you have worked with form validation before, you will be happy to know that starting with ASP.NET 2.0, .NET added exciting new features that takes care of form validation without too much complication on your end. In this article I will try to show and explain what these are and how you can use them.

  • Validate Empty Text – The ValidateEmptyText property fixes an issue with CustomValidator. In ASP.NET 1.0 custom validation would not fire if ValidationText was empty. You can set this property to true to cause custom validation to occur for empty input values.
  • Culture Invariant Values – When doing conversion on a compare validator’s non strongly-typed properties (CompareValidator.ValueToCompare, RangeValidator.MaximumValue, RangeValidator.MinimumValue) the validator will use a culture neutral format (Date: YYYY/MM/DD, Double & Currency: US culture format) to do the conversion when CultureInvariantValues is true.
  • Set Focus on Error – Using the new Focus API feature in ASP.NET validators can be configured to set focus to their associated control to be validated when a validation error occurs. The first control in the form that has a validation error will receive default focus when the form is submitted.
  • Validation Groups – You can assign a group name to a set of validators to ensure that validation occurs only for controls in the specified group. This enables you to have multiple control groups that are validated separately on the same page.

The new 3.5 framework includes a set of validation server controls that gives you an easy-to-use but powerful way to check input forms for errors and, if necessary, displays customized error messages in the page. Validation controls are added to a Web Forms page like other server controls. Of course there are controls for specific types of validation, such as range checking or pattern matching, plus a RequiredFieldValidator or cheking for regular expressions that ensures that 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 both that an entry is required and that it must contain a specific range of values, this is very handy and shortens your development time.

Here are some HTML controls that you may validate.

Control     Validation Property
HtmlInputText     Value
HtmlTextArea     Value
HtmlSelect     Value
HtmlInputFile     Value
TextBox     Text
ListBox     SelectedItem.Value
DropDownList     SelectedItem.Value
RadioButtonList     SelectedItem.Value
FileUpload     FileName

The most common one is RequiredFieldValidator, this is used to check if a field is empty or a checkbox is not checked.

here is an example:

<asp:RadioButtonList id=RadioButtonList1 RepeatLayout="Flow" runat=server>
<td align=middle rowspan=1>
<ControlToValidate="RadioButtonList1"  Display="Static"  InitialValue="" Width="100%" runat=server>

By using a these available controls you can save a lot of time in creating your validation forms.