Friday, September 30, 2011

ASP.NET Validation Controls with example


Learning Technique for freshers : R3C2

1.RequiredFieldValidation Control
2.RangeValidator Control
3.RegularExpressionValidator Control
4.CompareValidator Control
5.CustomValidator Control

Properties of Validation Controls.

ControlToValidate - Id of control Which you want to validate.
ErrorMessage - Message that will be displayed in the validation summary.
IsValid - Whether or not the control is valid.
Validate - Method to validate the input control and update the IsValid property.
Display - How the error message will display.options are given below
None:Message is never displayed.
Static:Validation message will display in the page layout.
Dynamic:Validation message is dynamically added to the page if validation fails.

Example with code:

1.RequiredFieldValidation:It is simple validation control which checks data is entered or not.

<asp:textbox id="txtEmpName" runat="server"/>
<asp:RequiredFieldValidator id="rfvtxtbox" runat="server" ControlToValidate="txtEmpName"
ErrorMessage="* Please enter the employee name" Display="dynamic">*
</asp:RequiredFieldValidator>

2.RangeValidator:It checks to see if a control value is within a valid range or not.

<asp:textbox id="txtDOB" runat="server"/>
<asp:RangeValidator id="rangeVal" runat="server"
ControlToValidate="txtDOB1"
MaximumValue="12/06/2009"
MinimumValue="01/01/1983"
Type="Date"
ErrorMessage="* Please enter the valid date" Display="static">*</asp:RangeValidator>

3.RegularExpressionValidator:It can be used for email validation or any specified string based on pattern matching.

E-mail: <asp:textbox id="txtEmail" runat="server"/>
<asp:RegularExpressionValidator id="revemail" runat="server"
ControlToValidate="txtEmail"
ValidationExpression=".*@.*\..*"
ErrorMessage="* Please enter valid e-mail ."
display="dynamic">*
</asp:RegularExpressionValidator>

4.CompareValidator: It allows you to make comparisons between two form controls and also compare values contained with
these controls to constants that specified by userd.

New Password: <asp:textbox id="txtNewPass" runat="server"/><br />
Confirm Passowrd: <asp:textbox id="txtConfirmPass" runat="server"/><br />
<asp:CompareValidator id="valCompare" runat="server"
ControlToValidate="txtNewPass" ControlToCompare="txtConfirmPass"
Operator="Equals"
ErrorMessage="* New Password and confirm password are not same"
Display="dynamic">*
</asp:CompareValidator>

5.CustomValidator:It allows you to write your own server-side or client-side validations logic and it can be easily applied to your web forms controls.

<asp:CustomValidator ID="cv" runat="server"
ControlToValidate="txtName" ClientValidationFunction="customvalidationr"
ErrorMessage="Please enter correct name"></asp:CustomValidator>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" />

Client side javascript

<script type="text/javascript">
function customvalidation(oSrc,args)
{

write your code here
}
</script>