Friday, April 26, 2013

Asp.net validation controls

Title:How to use validation control in asp.net

Description:
Normally in web application, validation are perform using java script. We must write JS and perform validations.If validations are successful then we redirect the user to sever and if validation fails then we display error to user to perform all these with java script. It is complex and also time consuming.In previous articles i explained how to pass server side variable to client side in asp.net,Validate drop down list in java script,How to validate text box in JQuery

Asp.net developers are provided with validation controls which enable user to perform validation just like server side controls.These validation controls render Jquery to the client or browser like this we can have rapid application development for validation and also we can reduce the Complexity of writing java script code validation controls of asp.net target clients as automatic which means based on browser validation are perform

The fallowing validation controls are supported by asp.net
1.Required Field validator
2.Range validator
3.Compare validator
4.Regular expression validator
5.Custom validator
6.Dynamic validator
7.validation summary

The most common property for validation controls:
Controltovalidate:web control id
Error message =message
setFoucsError=true/false
validation Group :makes the control part of separate validation group
Display:static(default)/dynamic/none
i.static :Initially place for displaying error will be reserved.When error occurs it will be visible otherwise it will be hidden
ii.Dynamic:No place in the form will be reserved initially only on error dynamically the message will be shown
iii.none:will not display the error message only

Required field validator:

Used to check null values and it is the only control that checks null values .Remaining validation controls doesn't perform any validation if null values are entered.IN the below example have used this validation for Textbox(txtOrder) which is used to give the Order value.
<asp:requiredfieldvalidator controltovalidate="txtOrder" display="dynamic" errormessage="Enter Order Details" id="RfOrder" runat="server">* 
</asp:requiredfieldvalidator>

Range validator:

To check for range of values.Useful for numeric and date format of data Additional properties Minimum value :1
maximum Value :1000

<asp:textbox id=" runat="server" txtage=""> <asp:RangeValidator id="RvAge" runat="server" ControlToValidate="txtage" 
 MaximumValue="16" 
 MinimumValue="24" 
 Type="Integer" 
 ErrorMessage="Enter valid age" Display="Dynamic">*</asp:RangeValidator>
Can one control have multiple validation controls? the answers is yes
we can use one validation control validate single controls only

Compare validator:

With compare validation we can perform 3 types of validation all will be based on comparison
1.Control with another control
2.control with value
3.control with data type

 UserName<asp:textbox id="txtuser1" runat="server"/>
 Reenter UserName: <asp:textbox id="txtuser2" runat="server"/>

<asp:CompareValidator ControlToValidate="txtuser1" ControlToCompare="txtuser2" Operator="Equals" ErrorMessage="Please enter valid user name" Display="dynamic">*</asp:CompareValidator>

validation summary control:

Used to report errors as a summary i:e display all control errors.error message display throw summary control.It has two properties
i)show summary :true/false
ii)show messageBox =true/false
when display is set to none then validation control will not display error in its place instead if displays error in summary control .

Regular expression validator:

Using they control we can specify on expression and ask validator to match the expression with control.Previously i have given to validate email using regular expression in asp.net.

Custom validator:

In previous post i have given example on how to use custom validator in asp.net.For complete details click here

No comments:

Bel