Validation Controls in ASP.NET

By
The Validation controls are used for validating the data entered in Text Box(input) controls. When any user enters the data on a text Box (web page) and submits the page on the server ,then validation controls are used to check the data field  entered  by the user .if  any data entered by the user  is invalid (not correct format) then validation control displays  an error message on the screen. All the error message is defined as properties values of validation controls. All the validation controls comes under System.UI.Webcontrols Namespace.
There are some validation controls ,that are used in ASP.NET.Which are given below :
  1.  The RequiredField Validator control
  2. The RangeValidator control
  3. The RegularExpressionValidator control
  4. The CompareValidator control
  5. The customValidator control
  6. The Validationsummary control
1. ) The RequiredFieldValidator control:-
It is used to check that there must be some value specify within the control.
Properties:- Some important properties are given below:-
  • ControlToValidate:- It is used to set the control field (text box) for validation.
  • Initial value:- It is only guidance for user which display default in the control.This property can be used for drop down list.
  • Text :- It is used to set the text value (Name) to the validation control.
2. ) The RangeValidator control:-
It checks whether or not the value of an input control is inside a specified range of values.This controls can be used with mobile Number,Age,Date of Birth etc. on the websites. I will discuss later this with the help of  a real time example.
There are  some important properties of RangeValidator control which are given below:-
  • ControlToValidate:- It is used to set the specific control to validate.
  • Minimum Value:- It is used to hold the minimum value of the valid range.
  • Maximum value:-It holds the maximum value of the valid range.
  • Type:- You can set Type properties also after the above properties if required.                                        String --> For a string data type                                                                                                                      Integer --> For a Integer Data type                                                                                                                Double:--> For A double data type                                                                                                                currency --> For currency data type                                                                                                            Date    --> For a date data type
3. ) The CompareValidator control:-
It is used to compare the values of two fields whether  it is same or not.If both controls values are not same then it will show error message.I will implement this control below with one real example.
There are some operator property also that is used for different type of comparisons.
  • Equal:-It is used to check whether the compared values are equal.
  • Not Equal :- It is used to check that control are not equal to each other.
  • Greater than --> It is used for greater than relationship.
  • GreaterThanEqual:- It is for  GreaterThanEqual relationship.
  • LessThan--> It is for less than  relationship.
  • LessThanEqual:- It is for less than equal relationship.
4. ) The RegularExpressionValidation control:-
It is use to check whether or not the text matches a certain pattern.There are some elements that used to make expression,which are given below:-
\d --> [0->9] .it takes value zero to nine.
\D--> Other than [0->9].
\w --> [a->z][A->Z][0->9]
\s --> space
\S--> other value except space.
{Length}-->{min ,max}
[ ] --> choice of given character(one out of them).
( ) --> group of validator
|  --> OR

We can use special characters which are given below:
* -->  0 -> more numeric values
+ -->  1 --> more numeric values
? -->  0(min) or 1 (max)

Note:- This is used to specify the preceded character,how many time used.
5. ) The CustomValidator control:-
This validation control is used  to customize and implement data validation according to our condition and requirement.
Ex.
Suppose, if  we  want to check given number is even or odd then we can not use our existing controls.so that ,to solve this type of problem we can use customvalidation control in ASP.NET.
There are some properties of custom validator controls which are given below:-
  • ValidateEmptyText -->It is used to set a Boolean value indicating whether empty text should be validated or not.
  • ClientValidationFunction:->It is used to set the name of custom client -side script function used for validation.
There are some other important property that can be used for validation.Which are given below:-
  •  "IsValid" property of page class:-                                                                                                        If all the validations controls are IsValid property will be true,page class property also contains true.But if only one validation control IsValid property is false then page class property will also contains false.
Ex.

Button click.....................
{
    if(Page.IsValid)
  {
   Label1.text="No Error";
  }
  else
  {
   Label1.text="Error";
  }
}

Note:- If any validation is failed then code will not executed.
  •  CauseValidation property of button control.                                                        
Every button has this property which contains by default 'True'  value that means if we click on Button then all the validation on the form will fired.If we don't want to validation check on the button click then we can change this value false.
The Button control and validation control has a property called Validation Group in which we specify the group name.

6. ) The Validation Summary control:-
This control is mostly less used.It is used to collect all the validation control error messages and display it collectively on the screen.
There are some important property of validation summary control
  • DisplayMode:- It is used to set the display mode of the validation summary control.
  • Forecolor:- It is used to set the foreground color of the control.
  • HeaderText:- It is used to set the header text displayed at the top of the summary.
Now i am going to Explain above Validation Controls with one Real time examples.There are some steps which are given below:-

Step1:- First open yore visual studio-->File-->New-->project--> Select ASP.NET Empty Website-->OK-->Solution Explorer-->Add New Web form-->and drag and drop Label,Text Box ,Button and Validation controls  as shown below:-


Home page

Step2:- Now change then following things which are given below for every control:-

 1. ) RequiredFieldValidator:- Go property of this control and change:-
      ControlToValidate --> Text Box, Which you want to validate.
      Error Message --> Which you want to show on the screen.
      Text  --> * (mandatory field),if required
 2. ) RegularExpressionValidator:- Go property of this control and change:-
       ControlToValidate --> Text Box, Which you want to validate.
      Validation Expression --> Use predefined Regular Expression or custom
     Error Message --> Which you want to show on the screen.
      Text  --> * (mandatory field),if required
 3. ) CompareValidator :- Go property of this control and change:-
        Control To Compare-->Which control you want to compare.
      ControlToValidate --> Text Box, Which you want to validate.    
        Error Message --> Which you want to show on the screen.
       Text  --> * (mandatory field),if required
 4. ) RangeValidator :- Go property of this control and change:-
     ControlToValidate --> Text Box, Which you want to validate.    
       Error Message --> Which you want to show on the screen.
     Maximum Value:-Enter Maximum value limit
       Minimum Value :- Enter Minimum value limit.
       Text  --> * (mandatory field),if required
After the changes you can see the page which are given below:-

properties


Step3:- Now Run the Application (press F5)-->and enter the required field as shown below:-


Run
Step4:-  Now Click the validate button--> then you will see the following output.


msdotnet

Note:- if you want to understand the  whole concept of validation control then first download this application from below and Run  this on your visual studio 2010 or other.You can  easily see each control's properties value which i have already  set.
For More:-
  1. Make Composite controls
  2. Create Captcha image
  3. Create setup file with database
  4. File handling Real application
  5. How to host ASP.NET Website on the server free
  6. Microsoft Sql server
  7. Take Print reciept from asp.net application
To Get the Latest  Free Updates Subscribe
                           Download

0 comments:

Post a Comment

Powered by Blogger.