Working with Validation Controls – Part 2

In the previous article, you learned how to perform validations using CompareValidator, CustomValidator and RangeValidator provided with the .NET Framework. In this second part, I will examine the applications of remaining three controls namely RegularExpressionValidator, RequiredFieldValidator and ValidationSummary.

While the ValidationSummary control summarizes all the errors occurred, the remaining two controls are used in different circumstances in a project depending upon the requirements.

RegularExpressionValidator

This validation control is used to parse strings using special characters. This is similar to the concept of Pattern Matching in PERL. This control can be used in situations such as:

(1) To check for "@" character on an e-mail address (See Listing 1)
(2) To verify whether the user entered valid Zip code, Telephone number and web address.

Listing – 1

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="background-color: #ffff00; color: black">&lt;%@ Page Language=&quot;VB&quot; %&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">title</span><span style="color: #0000ff">&gt;</span>Validation Controls in Action<span style="color: #0000ff">&lt;/</span><span style="color: #800000">title</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">form</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
Enter your e-mail address: 
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;TextBox1&quot;</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">Button</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;Button1&quot;</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">Text</span>=<span style="color: #0000ff">&quot;Click here&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">Button</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RegularExpressionValidator</span> 
<span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;RegularExpressionValidator1&quot;</span> 
<span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> 
<span style="color: #ff0000">ValidationExpression</span>=<span style="color: #0000ff">&quot;\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*&quot;</span> 
<span style="color: #ff0000">ErrorMessage</span>=<span style="color: #0000ff">&quot;Please enter a valid E-mail ID&quot;</span> 
<span style="color: #ff0000">ControlToValidate</span>=<span style="color: #0000ff">&quot;TextBox1&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RegularExpressionValidator</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">form</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span> 

It is the ValidationExpression property shown in the above listings that makes the application work. Hit the click here button without entering the @ symbol and observe the output (see Figure 1). The interesting point to note is that even if you enter @ symbol, it would still show errors, till you fill out the full domain part (like yahoo.com) of the email address. It is these powerful capabilities that made this control the most popular among .NET developers and communities.

READ  Silicon Valley Code Camp Next Weekend! Intro to .NET Core & ASP.NET 5

If you want to validate an Internet URL, simply update the ValidationExpression property in the above code with the listing as shown below:

Listing 2

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">http://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)? 

Moreover, you can also write custom validation statements depending upon your project requirements.

TIP: You can easily generate the code for the ValidationExpression property with the help of Visual Studio .NET or ASP.NET WebMatrix. Simply place the control from the Toolbox and press F4 to get the properties window. From the window, move down until you see the ValidationExpression property and click the small ellipsis button on the extreme right to open the Regular Expression Editor window. From this window, you can select your choice to get predefined expression statements for a number of items.

RequiredFieldValidator

As the name denotes, this control is used in cases where a user should compulsorily enter a value, preferably in a textbox. Prior to .NET, this kind of functionality was achieved through complex JavaScript and VBScript functions. Enter the code given in the listing given below, execute and observe the result:

Listing 3

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="background-color: #ffff00; color: black">&lt;%@ Page Language=&quot;VB&quot; %&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">form</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
Your Name: 
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;TextBox1&quot;</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">Button</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;Button1&quot;</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">Text</span>=<span style="color: #0000ff">&quot;Click here&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">Button</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span> 
<span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;RequiredFieldValidator1&quot;</span> 
<span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> 
<span style="color: #ff0000">ErrorMessage</span>=<span style="color: #0000ff">&quot;The above field should not be left blank&quot;</span> 
<span style="color: #ff0000">ControlToValidate</span>=<span style="color: #0000ff">&quot;TextBox1&quot;</span> 
<span style="color: #ff0000">display</span>=<span style="color: #0000ff">&quot;static&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">form</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span> 

Validation Summary

With the help of this control, you can display the error messages on a page. It collects all the error messages from the enabled controls and shows them in various ways like list, bulleted list, and single paragraph (See Figure 2). It also displays the errors in a message box. The code shown in listing 4 examines the application of this control:

READ  Understanding ASP.NET User Control

Listing 4

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="background-color: #ffff00; color: black">&lt;%@ Page Language=&quot;vb&quot; %&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">form</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
Enter a first value: 
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;txtValue1&quot;</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;Reqfield1&quot;</span> 
<span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> 
<span style="color: #ff0000">ErrorMessage</span>=<span style="color: #0000ff">&quot;The above field should not be left blank&quot;</span> 
<span style="color: #ff0000">ControlToValidate</span>=<span style="color: #0000ff">&quot;txtValue1&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
Enter a second value: 
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;txtValue2&quot;</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;reqField2&quot;</span> 
<span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> 
<span style="color: #ff0000">ErrorMessage</span>=<span style="color: #0000ff">&quot;The above field should not be left blank&quot;</span> 
<span style="color: #ff0000">ControlToValidate</span>=<span style="color: #0000ff">&quot;txtValue2&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">Button</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;btnClick&quot;</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">Text</span>=<span style="color: #0000ff">&quot;Click Here&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">Button</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">ValidationSummary</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;Validsummary&quot;</span> 
<span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">HeaderText</span>=<span style="color: #0000ff">&quot;Errors are as follows:&quot;</span> 
<span style="color: #ff0000">ShowMessageBox</span>=<span style="color: #0000ff">&quot;True&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">ValidationSummary</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">form</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span> 

As mentioned above, the above listing also displays a message box upon execution since the ShowMessageBox property is set to true.

READ  Saving Data From Gridview Footer In ASP.NET

From the above discussion, it is clear that all the required stuffs for validating Graphical User Interface (GUI) controls is now available under a single umbrella of Microsoft .NET. Once you learned the usage of all the controls, it is very easy to implement them, be it a WinForm or an ASP.NET WebForm.

One Response to "Working with Validation Controls – Part 2"

  1. Clifton   August 14, 2014 at 11:20 pm

    I see you share interesting content here, you can earn some additional money,
    your website has big potential, for the monetizing method, just type in google – K2
    advices how to monetize a website

    Reply

Leave a Reply

Your email address will not be published.