.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani
Sohaib Khalid

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Validation Controls used in ASP.NET

Posted By:Dhivakaran       Posted Date: October 22, 2010    Points: 5    Category: ASP.NET    URL: http://www.dotnetspark.com  

Validation controls allows you to validate a control against a set of rules. There are 6 different validation controls used in ASP.NET

RequiredFieldValidator - Checks if the control is not empty when the form is submitted.

CompareValidator - Compares the value of one control to another using a comparison operator (equal, less than, greater than etc).

RangeValidator - Checks whether a value falls within a given range of number, date or string.

RegularExpressionValidator - Confirms that the value of a control matches a pattern defined by a regular expression. Eg: Email validation.

CustomValidator - Calls your own custom validation logic to perform validations that cannot be handled by the built in validators.

ValidationSummary - Show a summary of errors raised by each control on the page on a specific spot or in a message box.

we can set the below new properties 

What's New in 2.0

  • Validation Groups - You can assign a group name to a set of validators to ensure that validation occurs only for controls in the specified group. This enables you to have multiple control groups that are validated separately on the same page.

  • Set Focus on Error - Using the new Focus API feature in ASP.NET validators can be configured to set focus to their associated control to be validated when a validation error occurs. The first control in the form that has a validation error will receive default focus when the form is submitted.

  • Culture Invariant Values - When doing conversion on a compare validator's non strongly-typed properties (CompareValidator.ValueToCompare, RangeValidator.MaximumValue, RangeValidator.MinimumValue) the validator will use a culture neutral format (Date: YYYY/MM/DD, Double & Currency: US culture format) to do the conversion when CultureInvariantValues is true.

  • Validate Empty Text - The ValidateEmptyText property fixes an issue with CustomValidator. In ASP.NET 1.0 custom validation would not fire if ValidationText was empty. You can set this property to true to cause custom validation to occur for empty input values.
Some examples:

Working with CompareValidator

The CompareValidator server control compares the values of two controls. CompareValidator uses three key properties to perform its validation. ControlToValidate and ControlToCompare contain the values to compare. Operator defines the type of comparison to perform--for example, Equal or Not Equal. CompareValidator performs the validation by evaluating these properties as an expression, as follows:
If the expression evaluates true, the validation result is valid. You can optionally specify the ValueToCompare property to compare against a static value instead of ControlToCompare. 

The CompareValidator server control could also be used to do Datatype validation.For example, if birth date information has to be collected from a user registration page, CompareValidator control could be used to make sure that the date is in a recognized format before it is submitted to the database. 

Working with Regular Expressions

The RegularExpressionValidator server control checks that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on. 

RegularExpressionValidator uses two key properties to perform its validation. ControlToValidate contains the value to validate. ValidationExpression contains the regular expression to match. 

Working with RangeValidator

The RangeValidator server control tests whether an input value falls within a given range. RangeValidator uses three key properties to perform its validation. ControlToValidate contains the value to validate. MinimumValue and MaximumValue define the minimum and maximum values of the valid range. 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend