Performing Validations with ASP.NET - Part 1
发表于2004/10/6 14:13:00 990人阅读
Validation means checking for accuracy and correctness of the entered data. By employing proper validations, you not only check for the above-mentioned criteria but also can prompt a user to enter a correct value and thereby avoid costly errors. These errors can be either due to carelessness of users or due to some other reason.
If you are not employing validations, then if the server returns an error, it may be unrecognizable for a user. This could tempt the user to leave your site. You could be losing an order and a valuable customer. These difficulties can be avoided if the form had been properly validated. During development, proper rules must be set to let the user know what to do if a specified error occurs. The form should have to be validated in such a way that the user should compulsorily enter data without which, the data cannot be submitted to the database on the server. You can also employ a system whereby your user could enter data in a required format such as only 5 digits for a zip code or a phone number in (---) --- --- format.
With ASP.NET, these difficulties are eliminated. ASP.NET provides its own built-in controls for validations. You can easily place controls and write codes by using a text editor like Notepad or an IDE like Visual Studio .NET. Moreover, ASP.NET supports C#, Visual Basic .NET, and any other language in the Microsoft .NET family. ASP.NET provides six types of validation controls. They are
Each of these above controls can be linked to .NET Web Form controls like TextBox, Dropdownbox, ListBoxes etc. Even though there are wide ranges of controls for validation, it is up to you to decide upon which control to use. It also depends up on your project needs. In this first part of this series, you will learn the application of first three controls with code listings using VB .NET and C#.
As the name suggests, this control is used to compare the values of one control with another control. The comparison is done with the help of textboxes. The CompareValidator control has two important properties namely, Operator and Type. The Operator property is mainly used as a basis of comparison between two values (GreaterThan, LessThan, Equal, NotEqual etc) and Type property indicates the data type of the values like Integer, string etc.
To illustrate, copy or enter the code in Listing 1 and save the file with extension .aspx under the Inetpub/wwwroot directory. Be sure to install .NET Framework SDK or Visual Studio .NET before attempting this work.
I recommend that you use ASP.NET WebMatrix for coding and testing the listings shown on this article (See figure 1). If you are using this tool, then you need not worry about giving extensions, setting the language etc. The tool does the entire work for you. It also includes a built-in server, which can be activated upon pressing the F5 key. You can also set various properties from the IDE itself and the coding is automatically done in the background. Moreover, the tool does not eat up your valuable disk space and is easy to download (www.asp.net) and install. After all, it's free from Microsoft
Click here for larger image<%@ Page Language="VB" %>
<title>Compare Validator - VB.NET</title>
Enter a First Value:
<asp:textbox id="firstvalue" runat="server">
Enter a Second Value:
<asp:textbox id="secondvalue" runat="server">
<asp:CompareValidator id="cv" runat="server"
ErrorMessage="oops! Error occured"
The above listing returns the text specified in the ErrorMessage property, if the value of the first textbox is less than that of the second one (See Figure 2). You can also compare two strings by setting the Type property to String and by setting the Operator to Equal.
Click here for larger image
Sub ServerValidate (sender As Object, _
value As ServerValidateEventArgs)
Dim num As Int32 = Int32.Parse(value.Value)
If num Mod 2 = 0 Then
value.IsValid = True
value.IsValid = False
Finally, you should integrate the above function with a WebForm control via ClientValidationFunction and ServerValidationFunction properties as shown in listing shown below:
<asp:TextBox id=Text1 runat="server" />
<asp:CustomValidator id="CustomValidator1" runat="server"
Display="Static" Font-Name="verdana" Font-Size="8pt" foreColor ="blue"
ErrorMessage = "Not an even number">
You have to specify a client-side or server-side function, which will contain the logic behind the validation using the OnServerValidate property. If there are any parameters, you should also specify those with this function. You should be aware that validations performed on the client's computer require no round trip to server and hence reducing the valuable online time of users.
This control is used to accept a range of values. For instance, you can employ this validation control, if a user should have to input values within a range such as from 1 to 100 or from 50 to 150. It accepts two important properties, MinimumValue and MaximumValue, and also the Type property. The listing shown below illustrates the functionality of this control:
<%@ Page Language="VB" %>
<head><title> Range Validator - VB.NET</title>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
ErrorMessage="Please enter a number between 1 and 100"
In this article, you learned how to perform validations using CompareValidator, CustomValidator and RangeValidator Controls. In the next part of this article, I'll examine the usages of other three controls.
About the Author
Anand Narayanaswamy (Microsoft MVP) works as an independent consultant and runs NetAns Technologies (http://www.netans.com) which provides affordable web hosting services for the community. Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes articles, product and book reviews for various websites. He can be reached at email@example.com
Interview Questions for ASP.NET Part - 1
garcon1986 2011-08-17 18:20 749
理想的 ASP.NET AJAX (Part 1 - Client Centric)
javazhuanzai 2012-01-16 05:23 253
ASP.Net请求处理机制初步探索之旅 - Part 1 前奏
sunday_qh 2017-02-08 15:47 82
Asp.Net构架(Http请求处理流程) - Part.1
byondocean 2011-04-12 21:16 422
Programming with Microsoft Visual C++ .NET的光盘part1
jurik 2008-01-03 04:14 45
Asp.Net 构架(IHttpHandlerFactory 介绍) - Part.4
calvincjl 2013-01-08 16:23 489
ASP.NET MVC4 Web 编程.part1
bbptech 2014-05-20 17:21 2
erwenyisheng 2013-09-05 11:06 59