Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CompareValidator is always valid

Status
Not open for further replies.

RobS23

Programmer
Jun 4, 2001
161
GB
Okay, I admit in advance I am well out of practice.

But this is driving me mad. I want to check for a valid date on my webform. It can be US or UK/European formats so I'm adding .CultureInvariantValues = True.

But whatever gibberish I type in the textbox is accepted as valid.

What on earth am I missing?


Dim datecheck As New CompareValidator

With datecheck
.ControlToValidate = Me.visitdate.Text
.Type = ValidationDataType.Date
.Operator = ValidationCompareOperator.DataTypeCheck
.CultureInvariantValues = True
End With

If datecheck.IsValid Then
'Me.lblnodate.Visible = False
Stop
Exit Sub
Else
'Me.lblnodate.Visible ="Invalid"
stop
end if
 
I would start here
Code:
datecheck.ControlToValidate = Me.visitdate.[COLOR=red]Text[/color]
text is the value of the control, not the control itself
Code:
datecheck.ControlToValidate = Me.visitdate.[COLOR=blue]Id[/color]
second, this is better suited for the webforms markup than code behind since the values are static.
Code:
<asp:CompareValidator runat="server" id="..." ControlToValidate="TheDate" Operator="DataTypeCheck" Type="Date" CultureInvariantValues="True" />
<asp:TextBox Id="TheDate" runat="server" />

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for your prompting. Being bloody minded and always trying to find my own way of doing things I played with it and ended up, as is so often the case, coming up with a hybrid solution at 3.00am.


1.EnableClientScript="False" on the webform I could then
call DateCheck.Validate() in my code at will

2. This also meant that I could wrap it in an If block of related controls. See if the 'visit' chkbox is checked and whether 'visitdate' was an empty string then set DateCheck.IsValid = False before calling DateCheck.Validate() This way I could then change the error message to something user friendly set focus and so on at will.

Once again thanks for your answer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top