ASP.NET Validation Controls Part 4

RequiredfieldValidator

Below we have a textbox control ‘email’ that will retrieve the user’s email address and a RequiredFieldValidator. The RequiredFieldValidator’s ControlToValidate property is set to the id of the textbox control. This associates the validator with the input control and ensures that a value must be entered otherwise the Text property will display it's message.

<asp:Textbox id="email" runat="server" />
<asp:RequiredFieldValidator id="rfv_Email" ControlToValidate="email" Text="You must insert an email address" runat="server" />

RegularExpressionValidator

Be aware in the example above that as long as the user enters a value then a validation error won’t be raised. The problem with that is the user could still enter an incorrectly formatted email address. The RegularExpressionValidator ensures that the value entered follows a specified pattern or format.

The example below ensures that a value is entered using the RequiredFieldValidator and that a specified pattern is followed using the RegularExpressionValidator. Both controls have their ControlToValidate properties set to the id of the textbox email.

<asp:Textbox id="email" runat="server" />
<asp:RequiredFieldValidator id="rfv_Email" ControlToValidate="email" Text="You must insert an email address" runat="server" />
<asp:RegularExpressionValidator ID="rev_Email" runat="server" ControlToValidate="Email"
Text="<br />Please enter a valid e-mail address."       
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />      

The RequiredFieldValidator and RegularExpressionValidator partner-up ensuring an email is entered and a properly formatted one at that.        

If you are using Visual Web Developer you can select from a range of regular expressions that are already provided.

If you select the validation control in the VWD designer, and then look in the properties window you can select the elispsis next to the ValidationExpression property.

Validation Expression

You can then select from a list of common regular expression functions as the screenshots below demonstrate.

Regular Expression

Read Part 3 Validation Control Properties

Read Part 5 The CompareValidator Control

Get the best asp hosting provider from web-hosting-top.com and save up to 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here