Exforsys.com
 
Home Tutorials VB.NET 2005
 

VB.NET Validation Controls

 

VB.NET Validation Controls

Page 1 of 3

VB.NET Validation Controls 


In this tutorial you will learn about User Input Validation, Required Field Validators, Comparison Validators, Range Validators, Regular Expression Validator, Custom Validators, ErrorProvider, Enabling Controls Based On Input and Other Properties of Validation.

 

Sponsored Links

 

User Input Validation


While any application can be designed with sound logic and good technology and can deliver high performance with accuracy, some errors could still creep into it. This could be due to wrong inputs by users. While the programmer may have taken care of all the exceptions it could cause a loss of business goodwill if a customer is confronted with an error message after he has input data into a number of fields. All of us are familiar with warnings like “Please enter a valid ZIP” or “Please Enter Your First Name!” and so on!


Thus some client side validations ensure that correct data is sent to the application. We can ensure such validations using validation controls. .NET Framework provides several controls for different types of validations.


The validation Controls that are available in .Net Framework are given below:


Control

Description

RequiredFieldValidator Ensures that the user enters data in the associated data-entry control
CompareValidator Uses comparison operators to compare user-entered data to a constant value or the value in another data-entry control
RangeValidator Ensures that the user-entered data is in a range between given lower and upper bounds
RegularExpressionValidator Ensures that the user entered data matches a regular expression pattern
CustomValidator Ensures that the user-entered data passes validation criteria that you set yourself

Required Field Validators


This is one of the simplest controls to use. This validating control makes sure that the users have entered data into a data-entry control. For example, you may want to make sure that users enter their mail id or their credit card number before they proceed to submit the form. The RequireFieldValidator control will ensure that the user will not be able to complete the form submission with null value for the field associated with this control.


The InitialValue property of this control has an initial value set to an empty String (“”) by default. The control raises an error message if this value does not change when validation occurs.


The other controls do not perform validation if the data entry field is empty and make it appear that the validation succeeded when no validation check has been performed. Therefore, it is imperative that a validation check be performed before other checks are activated.


Comparison Validators


This control is used to validate the value entered in to one data entry control by comparing it with the data entered in to another control. The ControlToValidate property sets the field to be validated. The ControlToCompare property specifies the control to compare with. You can also validate the data from constant value by setting the property ValueToCompare. When you set both the ControlToCompare and ValueToCompare then ControlToCompare takes precedence.


The Operator property sets the type of comparison that will be performed.


Table showing the values for Operator property:


Value

Description

Equal Checks if the comared values are equal
NotEqual Checks if the compared values are not equal
GreaterThan Checks for greater than relationship
GreaterThanEqual Checks for greater than or equal relationship
LessThan Checks for Less than relationship
LessThanEqual Checks for less than or equal relationship
DataTypeCheck Compares the data types between the value entered into the data-entry control that is validated and the data type specified by the Type property

The type property can have any of the following values:


  • String
  • Integer
  • Double
  • Date
  • Currency

Range Validators


A range validator test is used to check if the value entered in the data-entry control is within a specified range of values. The property ControlToValidate is set to the control that contains the data which is to be validated. The property MinimumValue sets the minimum value of the range. The property MaximumValue sets the maximum value of the range. The property Type sets the date type of the values to be compared. All the types of comparisons discussed above are still valid for this also.


Regular Expression Validator


RegularExpressionValidator control is used to check if the value in a data-entry control matches a pattern defined by a regular expression. You can check even the format of the text entered. Regular expressions are generally made up of test with embedded codes that start with a backslash (). For instance a simple expression for checking for either a uppercase or lower case alphabet is given by the expression “ b[A-ZA-z]+b.


Custom Validators


This control allows the developer freedom to define his own validations. The property ClientValidationFunction property sets the name of function or script that will do the validation. This function takes two parameters. The first argument source identifies the source control to validate. The second argument arguments hold the data to validate.


The Causes Validation Property


CausesValidation is one of the public instance properties of the control class, which specifies whether all controls which require validation gets it when the control gets focus. It returns true if the control causes validation to be performed on any controls requiring validation when it receives focus and false otherwise.

 

Sponsored Links

 

Validating event occurs when the control is validating at the time when the control loses the focus if the control’s CausesValidation property is true. Any code executed in response to this event can be used to throw exception if any is found. Validated Event occurs when the control has completed validation. This event occurs if no exception was thrown in the validating event. Clearing up the error provider messages can be done here.


Next Page: VB.NET Validation Controls - Page 2


Read Next: VB.NET Creating and Managing Components Part 1



 

 

Comments


jaber_souqi said:

  Can I have more about The validation with ASP.NET 2.0
September 6, 2006, 5:28 am

ereemye said:

  Really good. But i have a problem with creating crystal reports. i used a wizard but it only displays the field titles with out the a vailable information in the database. Now i know am asking the right guy
i will be very happy
October 2, 2006, 2:35 am

kushal kanal; said:

  well .. i have readed the vb.net validation examples and i was impressed by the article which was putted on it ...
now i have learnt how to validated the fields in the project ...

well .. now i wanna like 2 know ... the registration form 's fields validation like a password ... etc... if any one knows it ... plz mail me the code of the registeration form validation on the rajahuja18@gmail.com
October 16, 2007, 4:01 am

Omar said:

  Thanks for the article on validation, the article helped very much. But I want to find out how to do d same procedure with radio buttons and combo box. Where the system forces the user to input correct values.
March 12, 2008, 8:30 am

senthil said:

  Its very nice to learn for beginners and also for me, Thank you. I am having more doubts from VB.NET validations controls coding in this part. But in ASP.NET we have controls to validate but here we write coding. Why don't we go to the component and get the validation controls and validate?
March 14, 2009, 3:35 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

 
 


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape