Browse DevX
Sign up for e-mail newsletters from DevX


Getting Started with ASP.NET : Page 6

Learn what it takes to begin building ASP.NET Web sites with Visual Studio .NET. This article will provide you with the knowledge you need to jumpstart your foray into the world of ASP.NET development.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Form Field Validation
While it's not one of the most glamorous programming tasks, validating user entries certainly is one of the most important. Unfortunately, it can also be quite time consuming. Microsoft recognized this and created an entire series of Web controls dedicated to making validation quicker and easier.

Interesting Fact #6: You can find out more about creating Regular Expressions and view additional examples in the Visual Studio .NET Help.
The Validation Server controls provide a number of validation capabilities. These capabilities include checking for required entries, ensuring entries match a specified input expression (a telephone number for example), ensuring an entry is within a specified range of values, and ensuring an entry is valid compared to a constant, another control's value, or a database value. Table 10 lists type of validation controls.

Table 10: Validation control types.




Used to make sure a control is not left blank.


Used to compare a user entry to a constant value, another control's property, or a value retrieved from a database.


Used to make sure a user entry is within a specified upper and lower boundary.


Used to make sure an entry matches a pattern defined by an expression.


Used to display any error messages generated by the validation controls on a page.

A validation control acts as a virtual big brother for another server control. Each validation control has a specific job to do for the control it's assigned to watch over. You can assign multiple validation controls to watch a single data entry control. For example, you can assign both a RequiredFieldValidator and a RegularExpressionValidator to watch over the same telephone number TextBox control.

There are three properties that are common to all validation controls: ControlToValidate, ErrorMessage, and Display. The ControlToValidate property specifies which control to watch over. The ErrorMessage property specifies the text to display if the validation fails. The Display property determines where to display the error message.

A validation control is invisible to the user unless it is displaying its error message text. The three settings for the Display property, Static, Dynamic, and None specify how and where to display the error message text. The default value, Static, causes the validation control to take up space even when the error message text is not displayed. This allows you to define a fixed layout for a page. Using the Dynamic setting, validation controls do not take up any space until the error message is displayed. This causes the layout of the page to change when the controls display their message text. The last value, None, is used in conjunction with the ValidationSummary control and suppresses the display of the error message in the validation control. You'll learn more about this setting when we cover the ValidationSummary control.

The Page.Validate method causes the validation controls to work their magic. You can call this method in one of two ways. You can manually call the method or you can set a button's CausesValidation property to True. If the validation fails, your application will display the text specified in the ErrorMessage for the validation controls that failed. With either method, checking the Page.IsValid property in the submitting button tells you if the entries passed the rules set by their validation controls.

The following code shows a button that has its CauseValidation set to True:

Private Sub btnExample_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnExample.Click 'Page.Validate() ? Call if ' CausesValidation property is set to False If Page.IsValid Then 'Code here if validation passes End If End Sub

Checking the Page.IsValid property allows you to determine if the page validation succeeded or failed. If all entries are valid, the Page.IsValid returns True, otherwise it returns False.

Now we'll take a look at how to use each of the validation controls.

You can use the RequiredFieldValidator control to guarantee that a user doesn't leave an empty control. As with all of the validation controls, you must specify which control the RequiredFieldValidator watches over.

The following HTML code shows a text box named txtPhone with reqPhone, a RequiredFieldValidator control watching over it:

<asp:TextBox id="txtPhone" runat="server"> </asp:TextBox> <asp:RequiredFieldValidator id="reqPhone" runat="server" ErrorMessage="You must enter a phone number!" ControlToValidate="txtPhone"> </asp:RequiredFieldValidator>

Figure 11 shows the same RequiredFieldValidator control set up in the Property window.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date