Sunday, October 19, 2008

A simple introduction to ASP.Net validators

What is a Validator
A validator checks the input of a control to verify that the value falls within criteria you specified.

The validator we added is a range validator. On this validator, we have set the following:
• ControlToValidate the ID of the control you are validating.
• The MininumValue and MaximumValue sets the range you want the input to be in.
• The Type is set to Integer.
• ErrorMessage is the error message to be displayed when validation fails
• Display is set to Dynamic because it will otherwise take up the space of the error message.
• EnableViewState is set to false since there is no reason to store viewstate for an validator.

When you hit submit and you enter a text value or a value that does not fall into 1-10, a red error text appear next to the control.
The problem is when you enter a blank value and hit submit, no error is display even though a blank value is not in the range 1-10.

Handling blank values
One issue to watch out for is that virtually all of the validators will not validate if the value is set to blank. This is why the range validator failed to catch the blank input even though a blank is not in the range of 1-10.

Read more..

No comments: