Wednesday, September 1, 2010

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>  

15 comments:

  1. Very nice article. I really enjoyed it reading. And it also cleared lot of my doubts about Validation controls. Check this link too its also having post with wonderful explanation on Validation controls...

    http://mindstick.com/Articles/3af3cb8e-3bc0-4fc1-a2cd-8ce971c65ef2/?Validation%20controls%20in%20ASP.Net

    Thanks Everyone!!

    ReplyDelete
  2. nice articles,thankyou

    ReplyDelete
  3. it's very useful.

    ReplyDelete
  4. its really good thanks for posting

    ReplyDelete
  5. good article.. which gives complete summary on validation controls..

    ReplyDelete
  6. useful to us.. but need ex.. for client side validator

    ReplyDelete
  7. its very nice and helpful tutorial.............thanks

    ReplyDelete
  8. Hi..very nice tutorial keep it up dude

    ReplyDelete
  9. Hi,

    This is very informative article. Thanks for sharing your knowledge. There are few links that also helpful for developers.

    http://mindstick.com/Articles/cb1ae170-021f-4fd0-bb53-2225744bb89f/?Regular%20Expression%20in%20ASP%20NET
    http://mindstick.com/Articles/c3825daa-a449-467d-9513-34a8232d498a/?Validations%20on%20Asp%20Net%20Control
    http://msdn.microsoft.com/en-us/library/ms972961.aspx
    http://msdn.microsoft.com/en-us/library/aa479013.aspx

    ReplyDelete
  10. Vice informative article regarding validation controls. Developers can also check the detailed article on this topic at http://www.webcodeexpert.com/2013/06/how-to-use-customvalidator-validation.html

    ReplyDelete
  11. It'sVery informative blog and useful article thank you for sharing with us , keep posting learn

    more about Dot net
    Dot NET Online Training Hyderabad

    ReplyDelete