Subscribe to Subscribers

Resources » Code Snippets » ASP.NET WebForms

Example of CustomValidator control


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Gold    Points: 6


Example of CustomValidator control



Custome Validator control is used to write method to handle the validatin.
Drag and drop two lable control ,one textbox control ,one button control and one
custome validator control in aspx page.

In Custome Validator:-

Set ControlToValidate Property equal to "txtUsername"
Set ServerValidate Property equal to "ValidateUser"
Set Text Property "A username must be between 8 and 16 characters"
ServerValidate property is used to specifies the name of the server side validation function to be executed.


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>

</head>
<body>
<form id="form1" runat="server">
<asp:Label runat="server" Text="Enter a username: " />
<asp:TextBox id="txtUsername" runat="server" />
<asp:Button ID="Button1" Text="Submit" runat="server"/>
<br />
<asp:Label id="mess" runat="server"/>
<br />
<asp:CustomValidator ID="CustomValidator1" ControlToValidate="txtUsername" ServerValidate="ValidateUser" Text="A username must be between 8 and 16 characters" runat="server"/>
</form>





</body>
</html>



In code behind


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Sub ValidateUser(ByVal source As Object, ByVal args As ServerValidateEventArgs)
If Len(args.Value) < 8 Or Len(args.Value) > 16 Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub


End Class

Related Resources:


Read related articles: Custom validator    Validators    


Did you like this resource? Share it with your friends and show your love!


Responses to "Example of CustomValidator control"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Send Mail with Attachment using ASP.Net and C#.net or VB.Net
    Previous Resource: How to create Scrolbar with in Div?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Example of CustomValidator control  .  

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.