• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

Custom Validation in a Class

This question kind of follows on from a previous one that was answered by a number of people and where I was offered really good advice from BurntSky.

I'm still endeavouring to move a lot of code from the script tag to classes so that the methods can be shared by a number of pages.

So now I'd like to share some custom validation amongst my pages. The validation routine is like this at the moment in the .aspx page:

    Sub StartDateValidator_ServerValidate(ByVal s As Object, ByVal e As ServerValidateEventArgs)

        Session("StartDate") = CDate(dropStartDay.SelectedValue & "/" & dropStartMonth.SelectedValue & "/" & dropStartYear.SelectedValue)
        Session("DateToday") = Date.Now
        Dim intDelay As Integer = DateDiff("m", Session("DateToday"), Session("StartDate"))

        If IsDate(Session("StartDate")) = False Then
            StartDateValidator.Text = "Invalid Date, Please Change"
            e.IsValid = False
        ElseIf Session("StartDate") < Now.Date Then
            StartDateValidator.Text = "Cannot commence in the past"
            e.IsValid = False
        ElseIf intDelay > 1 Then
            StartDateValidator.Text = "We can only issue a certificate 1 month in advance of the Commencement Date<br />"
            e.IsValid = False
        Else
            e.IsValid = True
        End If

    End Sub

How would I go about turning this into a method in my class?
0
gilkesy
Asked:
gilkesy
1 Solution
 
Bob LearnedCommented:
Something like this might work:

Public Function ValidDates(ByVal startDate As Date, ByRef messageOut As String) As Boolean

  ' Assume valid dates.
  Dim isValid As Boolean = True

 Dim intDelay As Integer = DateDiff("m", DateTime.Now, startDate)

        If Not IsDate(startDate)  Then
            messageOut = "Invalid Date, Please Change"
            isValid = False
        ElseIf startDate < Now.Date Then
            messageOut = "Cannot commence in the past"
            isValid = False
        ElseIf intDelay > 1 Then
            messageOut = "We can only issue a certificate 1 month in advance of the Commencement Date<br />"
            isValid = False
        Else
            ' Valid was assumed above, and everything is acceptable, so just return.
        End If

  Return isValid

End Function

Untested coding in the comment block :)

Bob
0
 
gilkesyAuthor Commented:
Thanks for your reply Bob. I had to shuffle a few things round but it does work. The code isnow:

    Public Shared Function ValidDates(ByVal startDate As String, ByRef messageOut As String) As Boolean

        ' Assume valid dates.
        Dim isValid As Boolean = True

        If Not IsDate(startDate) Then
            messageOut = "Invalid Date, Please Change"
            isValid = False
        ElseIf CDate(startDate) < Now.Date Then
            messageOut = "Cannot commence in the past"
            isValid = False
        Else
            Dim intDelay As Integer = CInt(DateDiff("m", DateTime.Now, startDate))
            If intDelay > 1 Then
                messageOut = "We can only issue a certificate 1 month in advance of the Commencement Date<br />"
                isValid = False
            End If
        End If

        Return isValid

    End Function


The function is called like this:

    Sub StartDateValidator_ServerValidate(ByVal s As Object, ByVal e As ServerValidateEventArgs)

        e.IsValid = GlobalMethods.ValidDates(dropStartDay.SelectedValue & "/" & dropStartMonth.SelectedValue & "/" & dropStartYear.SelectedValue, StartDateValidator.Text)

    End Sub
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now