Logo         Community
The Company
Your Account
Contact Us
Input Validation With ASP.NET (part 1)
Protect your Web application from bad data with ASP.NET's Validator objects.

| First Glance |

Enough theory. Let's get our hands dirty with some code, shall we?

<%@ Page Language="C#" %>
<title>What's In A Name?</title>
<basefont face="Arial">
<form runat="server" method="POST" >

<asp:label id="lblFirstName" runat="server" text="Your name, please? " /> <asp:textbox id="strFirstName" runat="server"/>
<br />

<asp:RequiredFieldValidator id="strFirstNameRFV" ControlToValidate="strFirstName" ErrorMessage="Don't you have a name?" runat="server"/>
<br />

<asp:button id="Submit" Text="Submit" runat="server"/>

In case you see errors about a missing "WebUIValidation.js" file instead of the form above, ensure that this file is under your Web server root, in the "\aspnet_client\system_web\1_1_4322" folder (you can locate the file by doing a quick search on your .NET development system), and then try again.

Now, submit the form without entering any data, and you should now see an error message on the screen.

There's nothing very exciting about the code above - the first few lines simply make use of two plain-vanilla server controls, a label and a textbox; the latter allows the user to enter his/her name. The meat of the script lies in this line:

<asp:RequiredFieldValidator id="strFirstNameRFV" ControlToValidate="strFirstName" ErrorMessage="Don't you have a name?" runat="server"/>

This is your very first ASP.NET Validator control. Aptly called the RequiredFieldValidator control, it forces the user to enter a value in the associated field. Here are the attributes I've used:

- The "id" attribute specifies a unique identifier for this control (useful if you want to script actions based on the state of this object).

- The "ControlToValidate" attribute associates this control with a form field. Since I want to validate the "strFirstName" textbox control, I've assigned it this value.

- The "ErrorMessage" attribute stores the message to be displayed to the user, if (s)he does not enter a value in the "strFirstName" textbox control.

- The mandatory "runat=server" attribute tells the .NET CLR that this control has to be processed at the server and not the client.

Now, you might be wondering whether the client or the server performed the validation. Take a guess!

And if you guessed "the client", you guessed right! However, note that this doesn't mean that server-side validation is disabled; if, for some reason, client-side validation fails, the server will leap in and enforce your validations instead. Flip the page, and I'll tell you a little more.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback