Custom textbox Validator doesn't?

Fade

Member
Joined
May 30, 2003
Messages
9
Location
Australia
Hi all,

There seems to be a complete black hole in the net when it comes to information about subclassing BaseValidator..

I have a webform with a bunch of textboxes I need to validate. Some are required text fields, others are date fields that are required, must be valid dates, and are in an expected format.

Im using the RequiredFieldValidator for the first and decided to create my own Validator for the second type, the DateValidator.

The EvaluateIsValid method of my DateValidator simply checks if the value exists and is a valid date (and I plan on using a regexp to check the format).

The problems are these:
1) My DateValidator works and displays its ErrorMessage when appropriate, but does not appear in a ValidationSummary list with ShowMessageBox=true

2) If i have any other standard validator present on the page (eg. the previously mentioned RequiredFieldValidator) that validator is processed first and, if it returns IsValid=false, the DateValidator does not validate. As far as I can tell, the EvaluateIsValid method is never called.

Ive spent a day looking for the missing bracket or closing tag but I just cant seem to figure it out..

If no one else has had similar problems, Ill post the code and see if someone can reproduce it.


Many thanks..

Fade
(the rapidly balding..)
 
Having thought about it further (and when its not 6pm on a friday..) I wondered if maybe theres some kind of order that validators get processed.. ie. if a RequiredField validator fails then no other validators are processed?? that would answer question 2 but still not the effect I hoped for since I essentially want to validate that a date is entered AND valid.

Either way, the error message from my subclassed validator shows in the validation list, but not in the message box.

heres the page (stripped down as much as possible):

<%@ register tagprefix="cms" namespace="CustomValidators" Assembly="CMSWebLib" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="CMS.WebForm1"%>
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body>
<form id="Form1" runat="server">
<table cellSpacing="0" cellPadding="2">
<tr>
<td>
<asp:textbox id="txtText1" runat="server" BackColor="White"></asp:textbox>
<cms:DateValidator id="Datevalidator1" runat="server" display="None" ControlToValidate="txtText1" ErrorMessage="Invalid Date 1"></cms:DateValidator></td>
<td>
<asp:textbox id="txtText2" runat="server"></asp:textbox>
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" Display="None" ControlToValidate="txtText2" ErrorMessage="Missing Date 2"></asp:RequiredFieldValidator></td>
<td>
<asp:textbox id="txtText3" runat="server" Width="100px"></asp:textbox>
<cms:DateValidator id="DateValidator2" runat="server" display="None" ControlToValidate="txtText3" ErrorMessage="Invalid Date 3"></cms:DateValidator>
</td>
</tr>
<tr>
<td>Date 1</td>
<td>Date 2</td>
<td>Date 3</td>
</tr>
<tr>
<td>
<asp:button runat="server" text="Validate" ID="btnValidate" />
</td>
</tr>
</table>
<asp:ValidationSummary id="ValidationSummary1" runat="server" ShowMessageBox="True"></asp:ValidationSummary>
</form>
</body>
</HTML>

and the Validator object defined in my CMSWebLib dll:

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text

Namespace CustomValidators

Public Class DateValidator
Inherits BaseValidator

Protected Overrides Function EvaluateIsValid() As Boolean

Dim strInputDate As String

strInputDate = Me.GetControlValidationValue(Me.ControlToValidate)

If Trim(strInputDate <> "") AndAlso IsDate(strInputDate) Then
Return True
Else
Return False
End If

End Function

End Class
End Namespace
 
Trim(strInputDate <> "") returns string, so it is quite incorret to put it in "if" statement. Maybe thats the point. BTW Trim() is VB time, shoud be "if (strInputDate.Trim.Length > 0)..."
 
hehe ok so a bracket was out of place after all.. trim(strInputDate) <> "" was what it was supposed to be (yes, spot the VB programmer) and would have worked too.. the .length property looks neater, but would not catch whitespace so .Trim.Length sounds good. Thanks hrabia!

Unfortunately that doesnt solve the problem.. In the sample above if you leave all 3 textboxes blank, only the message from the RequiredFieldValidator is displayed.

If a value is entered in the required field, the two DateValidators fire and correctly report invalid dates.. but only to the on-page summary list and not in the messagebox..
 
I think that your problem is where the validate occurs. In server controls validators default value of EnableClientScript is true, that means when you use IE4 or better fields are validate on client (javascript functions). That the reason why only RequiredFieldValidator occurs in MessageBox and summary list. You can set this property to false (RequiredFieldValidator2.EnableClientScript = False) and after that, all validation will be made on server. You can also emitt your own client side validate script but that no so easy, look at http://msdn.microsoft.com/library/d...onclient-sidefunctionalityinservercontrol.asp or you can write your own base validator implementing IValidator interface.
 
Having thought that through it makes a lot of sense..

Emitting your own script does look like a pretty involved process and is something Id like to look into but I dont think Ive got enough time before my deadlines so Ill switch everything over to server-side validation (for the sake of consistancy).

Thanks for all the help, Hrabia. If I have any luck with emitting client-side validation code in the future Ill post it here!
 
Back
Top