One of the many strengths of ASP.NET is that it provides a framework for common Web applications through structured interfacing.

Most business applications involve data input, validation, and some form of output. Web applications are no different.

HTML forms are created for users to enter data; however, data validation can occur through some combination of client-side scripts or server-side validation. The drawback to this is that putting that responsibility on an unknown client tool, the browser, can produce less than exemplary results.

Therefore, data should be validated on the server where you have full control of the outcome. This used to be handled by parsing individual request elements and running them through a validation mechanism. Without much direction, this validation mechanism can run amuck. However, ASP.NET's validation mechanism gives even amateurs the ability to validate data through a structured approach. This is done through validators. In this article, I'll describe ASP.NET's validators and explain how you can incorporate them in your pages.

There are five validators for user input: RequiredFieldValidator, RegularExpressionValidator, CompareValidator, RangeValidator, and CustomValidator.

  • RequiredFieldValidator checks to see if data was entered or selected for the input control.
  • RegularExpressionValidator uses a regular expression to validate the value of the input control.
  • CompareValidator checks the input control's value against a fixed value or another input control's value.
  • RangeValidator checks to ensure that the value of an input control is between two fixed values.
  • CustomValidator uses a custom routine to validate the input.

It's simple to use these validators in your page. Simply add one or more validator for each corresponding form element whose input you want to validate. You can also add output text to each validator that will display when an invalid condition has occurred. Once all the data has been validated against the validators, any invalid conditions can be reported back through the ValidationSummary control. This is a simple developer-defined list of invalid conditions. Here's the code that will accomplish all this:

And on your code-behind within your page class, add these two methods:

JavaScript is automatically created for you for client-side validation before submission. However, the final test for validity is done on the server. During the cmdSubmit_ServerClick() event, the IsValid property of the Page instance is checked. If all the validators pass their tests, then the Page is marked as valid and the event is carried out. In this case, "Success!" is simply written to the Response buffer and the Response is ended.

For each validator, a red asterisk will be placed beside the input if the value does not satisfy the validation attempt. Each validator's error message will be displayed in the ValidationSummary area. The CustomValidator is a bit different. If all the other validation attempts are successful, then the CustomValidator1_ServerValidate() event occurs. The input control's value is then checked to see if it equals 13. If so, the IsValid property is set to false. In order to test this event on this example, you need to set the compare text's value to 13; otherwise, the CompareValidator will exit and the _ServerValidate() event will not occur.

With a little crafty coding, a more complex validation mechanism can be constructed using ASP.NET's validators as a foundation. For more information on these validators, visit the MSDN Web site.

Do you need help with HTML? Gain advice from Builder AU forums

Related links

Comments

1

vijji - 20/07/06

I'd like to write an asp.net web application, in which I want to show a window or browser.

Does anyone have any code that could help me?

» Report offensive content

2

Xtreme Dare - 10/11/06

Actually i used the aspx controls in my web project and also it is also working fine .But there a problem with this control this validation is
affecting on the other hyperlinks on this site apart from the button
events.

» Report offensive content

3

fgfdg - 23/12/06

4

Keyur - 29/01/07

Can ne one please tell me how to validate data entered by using FormView or DetailsView....please...its urgent.

» Report offensive content

5

fghgf - 15/06/07

6

sfdghsgh - 21/09/07

7

srinivas - 16/10/07

Hi , assume that we are using a table at back end and we have to validate the user id and password from that table

» Report offensive content

8

nona - 16/02/08

9

nona - 16/02/08

adlfjdslfjl

2d8c222d8c222d8c222d8c22

» Report offensive content

10

nona - 16/02/08

<html>

2d8c222d8c222d8c222d8c22

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

10

nona - 16/02/08

<html>2d8c222d8c222d8c222d8c22 ... more

9

nona - 16/02/08

adlfjdslfjl2d8c222d8c222d8c222d8c22 ... more

8

nona - 16/02/08

adlfjdslfjl ... more

Log in


Sign up | Forgot your password?

What's on?