Sunday, December 7, 2008

Validation

There are so many ways to do validation these days and all have their benefits. I have been working with the built in .net Validation Controls and from this have defined a list of tips : 

Here are the built in controls you recieve from the framework



Here is the form I am dealing with :



Required Field Validator

This control is simply used to make sure another control, such as a textbox will error if the page tries to redirect without the textbox having anything entered in it.

Tips

Set the error message as something descriptive
Set the text to "*"
Set the ControlTo Validate as the Textbox you wish to control

Range Validator

This control makes sure that the control that this validator is controlling falls within a bounds. 

Regular Expression Validator

This control uses Regular Expressions to Validate another control

Tips

There are built int Regula
r Expressions for commonly used validation types e.g. Phone Numbers, Email Address' etc.

Compare Validator

This control is used to compare to controls to each other. This a great control to use for password and confirm password controls.

Custom Validator

This control is used for complex validation that the other validation controls cannot perform. For example, if you wanted to check if the number entered in a textbox is divisble by 2.

ASPX

CODE BEHIND

protected void IsDivisibleByTwo(object sender, ServerValidateEventArgs e)
{
 // Some Code
}

Validation Summary

I always use this control at the bottom of the page. It basically grabs all the validation controls on the page and displays their error messages in a summary format.

Hope this helps,

 - Tim



No comments: