Validation in ASP.NET
Published 09/21/01 - For ASP.NET
When you have a form that user's submit data,
its important that they don't mess it up and enter weird stuff. Validation
stops this but in ASP it was a tedious job of If statements to work out. I
myself had a simple form that added users to a database, it had a heck of a
lot of if statements. ASP.NET introduces - Validation Controls. These controls
are just like Web Controls and can do many things like - Check if a field is
filled out, Compare a field to something (or another), Use regular expressions
on a field, Check to see if a field is within a certain range, you can even
create your own!
Before we begin
Now, this is going to be a pretty big
article/tutorial so lets get some of the common stuff out first.
Validation Controls only work for the following
But what other controls could you use it on?
Yes there are many more, but its rather hard to do everything in one. Next,
the control syntax takes the form of -
(Remember that the italics stuff is stuff that you change).
Now its pretty obvious what ErrorMessage is (its the message that's displayed
if it turns out that the field isn't valid). ControltoValidate is the id of
the control that your going to be checking. One last thing,
here is a
page that I've created that uses Validator Controls. Try the following to see
what errors come up:
- Enter no name.
- Enter only a first name.