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 :
There are some important properties of RangeValidator control which are given below:-
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.
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:-
Note:- If any validation is failed then code will not executed.
There are some validation controls ,that are used in ASP.NET.Which are given below :
- The RequiredField Validator control
- The RangeValidator control
- The RegularExpressionValidator control
- The CompareValidator control
- The customValidator control
- 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
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.
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.
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
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:-
Step2:- Now change then following things which are given below for every control:-
1. ) RequiredFieldValidator:- Go property of this control and change:-
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.
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:-
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.
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.
Step3:- Now Run the Application (press F5)-->and enter the required field as shown below:-
Step4:- Now Click the validate button--> then you will see the following output.
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:-
Download
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:-Step3:- Now Run the Application (press F5)-->and enter the required field as shown below:-
Step4:- Now Click the validate button--> then you will see the following output.
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:-
- Make Composite controls
- Create Captcha image
- Create setup file with database
- File handling Real application
- How to host ASP.NET Website on the server free
- Microsoft Sql server
- Take Print reciept from asp.net application
Download
0 comments:
Post a Comment