Geeks With Blogs
Dheeman Dutta Just Another Blog.....

I had to validate the login page of my app. It had only two textbox es (UserId,Password) and a login button  on the page. There are quite a few validations I can think of
and some of them were duplicate for both boxes , whereas some validations include both boxes in their critera.I started think as to which controls I can drop to do the work fast.
I decided to to use the combination of RequiredFiedValidator,CustomValidator and VaidationSummary. There cam a few problems
 ** I could not use javascript in my CustomValidator as they are browser dependent ,also some validations were such that they could only
    be performed on the serverside. viz Invalid UserName or Password.
        ** I can use the CustomValidator's Validte Event to handle the above probelem , but, then I would need two CustomValidators hooking to
    each text box and each one making a trip to database and checking. (You may argue that a customvalidator hooked to the password box will server the purpose,
    but you can never presume that an user will always have the control on the password box and not on the UserID box when clicking the Login Button).The
    disadvantage with having two CustomValidators is that , then there will be duplicate coding which is anyway never prescribed.
        ** The bottomline of my problem is that I wanted to associate two contols to one Validator

 So what is the remedy to the soln..??? I still wanted to use the valdation feature that .NET Framework has provided us.
 The only way is to create a real custom validator that was still supported by the Summary control.

 To implement a validator:

 Step 1: Dropa ValidationSummary Control on page

 Step 2: You need to implement the IValidator Interface

 Imports Microsoft.VisualBasic
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Common
    Public Class ValidationMessage
        Implements System.Web.UI.IValidator

        Dim _message As String
        Dim _isValid As Boolean

        Public Sub New(ByVal msg As String)
            _message = msg
        End Sub

        Public Property ErrorMessage() As String Implements System.Web.UI.IValidator.ErrorMessage
                Return _message
            End Get
            Set(ByVal value As String)
                _message = value

            End Set
        End Property

        Public Property IsValid() As Boolean Implements System.Web.UI.IValidator.IsValid
                Return _isValid

            End Get
            Set(ByVal value As Boolean)
                _isValid = value
            End Set
        End Property

        Public Sub Validate() Implements System.Web.UI.IValidator.Validate

        End Sub
    End Class
End Namespace

Step 3: In the click event  of the button just perform your validations and in case of an validation failure, just add the errormessage to Validtors of the Page

  If Not objUserInfo Is Nothing AndAlso objUserInfo.Membership.LockedOut Then
                        strMessage = Services.Localization.Localization.GetString("UserLockedOut", Me.LocalResourceFile)
                        Page.Validators.Add(New ValidationMessage(strMessage))


Step 4:
 Protected Overrides Sub OnUnload(e As EventArgs)
    If Not (Page Is Nothing) Then
   End If

End Sub   

The Summary control takes over after this, and displays the message  ...


Posted on Thursday, November 23, 2006 6:19 AM | Back to top

Comments on this post: Create Your Own Validator

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Dheeman Dutta | Powered by: