Required Field and Validater

The following example shows a customer date picker which is required and need to be validate before the saving.

1. general using


   <BDP:BDPLite ID="BDPLiteDateAbsent" runat="server" />
   <asp:RequiredFieldValidator ID="RequiredFieldValidatorDateAbsent" runat="server" ErrorMessage=" * Required" ControlToValidate="BDPLiteDateAbsent" Display="Dynamic"></asp:RequiredFieldValidator>
   &nbsp;<br>
   <asp:CustomValidator ID="CustomValidatorDateAbsent" runat="server" ErrorMessage="* Date is not in the range of the selected sitting" ControlToValidate ="BDPLiteDateAbsent" Display ="Dynamic" OnServerValidate="CustomValidatorDateAbsent_ServerValidate">
   </asp:CustomValidator>

    Protected Sub CustomValidatorDateReceived_ServerValidate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
        Dim initialdate As New Date(1990, 1, 1)
        If (Date.Compare(CDate(args.Value), initialdate) > 0) Then
            args.IsValid = True
        ElseIf BDPLiteDateReceived.IsNull And BDPLiteDateReceived.IsDate Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub

2. Another way which can change the displayed error message when the event being triggered.

    Protected Sub CustomValidatorDateAbsent_ServerValidate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
        If datacontroller.VerifyDateAbsent(CDate(args.Value), Me.ID) Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If

        Dim CV As CustomValidator = CType(sender, CustomValidator)
        CV.ErrorMessage = datacontroller.getDateErrorMessage(Me.ID)
    End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s