Ensure data integrity with validation in ASP.NET
Takeaway: ASP.NET provides numerous validation controls that make it easy to validate data entered via a Web Form. Here are various ways to validate user-entered data via these controls.
Data collection is an integral part of most applications whether Web- or Windows-based. It varies from obtaining user feedback to user profile information. This data is often stored in a backend database system, which contains data constraints defining what is and is not allowed, so the data collected and sent to the database system must adhere to the constraints to avoid errors.
Validation dos and don'ts
The many languages available for working with the .NET Framework make it easy to develop code that validates data entered by a user before sending it to a database table. In addition, ASP.NET provides numerous validation controls that make it easy to validate data entered via a Web Form.
Though your .NET development toolbox is well-stocked, these tools are useless if you use them ineffectively. Be aware of what type of data checks you need to perform to ensure data integrity. The following list outlines how you should approach data validation:
Putting this list in action depends on the application type. We'll examine an example using the ASP.NET platform. The sample Web Form has four fields:
The complete Web Form source is listed next. It includes all fields and validation controls with a JavaScript button for submitting the form, thus triggering the validation:
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<html><head>
<title>Builder.com Data Validation
Example</title>
</head>
<body text="#000000" bgColor="#ffffff">
<form id="frmDataValidation" method="post"
runat="server">
<h1>Builder.com Data Validation Example</h1>
Username:
<asp:textbox id="userName" Runat="server"
MaxLength="50">
</asp:textbox>
<asp:requiredfieldvalidator id="userNameRequiredFieldValidator"
runat="server"
ErrorMessage="Please enter a username."
EnableClientScript="False"
ControlToValidate="userName"></asp:requiredfieldvalidator>
<br />
Zip code:
<asp:textbox id="zip" Runat="server" MaxLength="5"
Columns="7">
</asp:textbox>
<asp:regularexpressionvalidator id="zipRegExValidator"
Runat="server"
ErrorMessage="Please enter a valid zip code (00000 -
99999)."
EnableClientScript="False" ControlToValidate="zip"
ValidationExpression="^\d{5}$">
</asp:regularexpressionvalidator>
<asp:RangeValidator id="rvZipCode" runat="server"
ControlToValidate="zip"
ErrorMessage="Please enter a valid zip code (00000 -
99999)."
MaximumValue="99999" MinimumValue="00000" Type="Integer"
Visible="False">
</asp:RangeValidator>
<br />
Start date:
<asp:TextBox ID="txtStartDate" Runat="server" Width="75"
MaxLength="20"></asp:TextBox>
<asp:RequiredFieldValidator
ControlToValidate="txtStartDate"
EnableClientScript="true"
Runat="server" ErrorMessage="Please enter a valid start
date(mm/dd/yyyy)."
ID="rfvStartDate">
</asp:RequiredFieldValidator>
<asp:CompareValidator id="cvStartDate" runat="server"
ControlToValidate="txtStartDate"
Operator="DataTypeCheck" Type="Date" ErrorMessage="Please enter a
valid start
date
mm/dd/yyyy)." EnableClientScript="False">
</asp:CompareValidator>
<asp:CompareValidator id="cvStartDateLessThanEndDate"
runat="server"
ErrorMessage="Start date must be less than end date."
ControlToValidate="txtStartDate"
Type="Date" Operator="LessThanEqual"
ControlToCompare="txtEndDate"
Display="Dynamic">
</asp:CompareValidator>
<br />
End date:
<asp:TextBox ID="txtEndDate" Runat="server" Width="75"
AutoPostBack="True"
MaxLength="20"></asp:TextBox>
<asp:RequiredFieldValidator
ControlToValidate="txtEndDate"
EnableClientScript="true"
Runat="server" ErrorMessage="Please enter a valid end date
(mm/dd/yyyy)."
ID="rfvEndDate">
</asp:RequiredFieldValidator>
<asp:comparevalidator id="cvEndDate" runat="server"
ControlToValidate="txtEndDate"
Operator="DataTypeCheck" Type="Date"
ErrorMessage="Please enter a valid end
date
(mm/dd/yyyy)." EnableClientScript="False"
Display="Dynamic">
</asp:comparevalidator>
<br />
<input type="button" value="Submit" id="submitButton"
onclick="document.forms[0].submit();" />
</form></body></html>
This simple example demonstrates the various ways to validate data entered by a user via ASP.NET validation controls.
Check the data
Ensuring data integrity is important to proper application operation. User input should be properly validated to avoid application errors when the data is saved to a data source like a SQL Server database. One approach to application development preaches building test cases first before actual coding. This is a good technique for a user interface as well; it helps the developer recognize data validation opportunities.
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!
SponsoredWhite Papers, Webcasts, and Downloads
- How File Fragmentation Occurs on Windows XP / Windows Server 2003 Diskeeper
- Self-Tuning Disk Drives Eliminate Performance Bottlenecks and Heighten ROI Diskeeper
- The Road Ahead for Business Process Management SAP
- Webcast: How to Get the Most Out of Microsoft Windows Deployments with Intelligent iSCSI Storage Dell EqualLogic
- Defrag Myth Busters - What You Should Know Diskeeper
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
