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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple custom validation trouble...

Status
Not open for further replies.

song2siren

Programmer
Jun 4, 2003
103
GB
Hello

I just need to create a custom validator to check whether a control contains a value and, if it doesn't, change the style of div tag.

I tried this:

Sub surnameCheck_ServerValidate(sender as Object, e as ServerValidateEventArgs)
Dim strValue As String = e.Value
If not strValue is nothing then
e.IsValid = True
Else
e.IsValid = False
lblSurnameRow.Text = "<div class='row error'>"
End If
End Sub

With my controls like this:

<asp:Label ID="lblSurnameRow" runat="server"><div class="row"></asp:Label>
<span class="formElement"><asp:TextBox ID="frmSurname" onfocus="pviiW3Cbg(this,'#F4DFA9')" onblur="pviiW3Cbg(this,'#E5EFFF')" runat="server" /></span>
<asp:CustomValidator ControlToValidate="frmSurname" Display="Dynamic" runat="server" OnServerValidate="surnameCheck_ServerValidate" />

However, the form is submitted regardless of a value in frmSurname.

Hopefully, I'm missing something really simple, but any suggestions would be much appreciated.

 
Code:
if strvalue.length > 0 then
 E;isvalid = true
...

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Hi chrissie1 - thanks for the tip

I've changed my validation code to:

Sub surnameCheck_ServerValidate(sender as Object, e as ServerValidateEventArgs)
Dim strValue As String = e.Value
If strValue.Length > 0 Then
e.IsValid = True
Else
e.IsValid = False
lblSurnameRow.Text = "<div class='row error'>"
End If
End Sub

However, the validation still isn't running and my submit code runs as if the page is valid. The code to submit is as follows:

Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)

If Page.IsValid Then
etc
End If

End Sub

Just can't seem to stop the page submitting if this field is empty.

Thanks again.


 
I've not used VB.NET in a while, so I'm a bit rusty, but in C# you'd have to do:

SurnameCheck.Validate();
Page.Validate();
if (Page.IsValid.....
etc

I think you have to explicitly call the control's validate method.

HTH, apologies if my VB.NET memory is fooling me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top