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

Custom Validator not firing 2

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Hi guys, I am trying to set up a custom validator but it does not seem to work. My required field validators all work but not this.
For the sake of testing the validator is being set to just return false

here is my code aspx

Code:
              <input type="text" id="ad_year" name="ad_year" runat="server" />
			  	<asp:CustomValidator id="CustomValidator1"
				ControlToValidate="ad_year"
				OnServerValidate="ServerValidation"
				ErrorMessage="Not a number!"
				runat="server"/>

here is the c#
Code:
    void ServerValidation (object source, ServerValidateEventArgs args)
      {           
            args.IsValid = false;
      }

any ideas why this does not work?
 
It's because validators are generally designed to work client-side - try inserting your validation sub right in the HTML view. Sorry my example is VB, it should be easy to change if necessary.

Code:
<script language="vb" runat="server">
Sub validateme(sender as Object, args as ServerValidateEventArgs)
   args.IsValid = False
End Sub
</script>

<asp:CustomValidator id="CustomValidator1" style="Z-INDEX:101; LEFT: 424px; POSITION: absolute; TOP: 240px" runat="server" ErrorMessage="error!"  ControlToValidate="TextBox1" OnServerValidate="validateme"></asp:CustomValidator>
 
dace - won't that still run server-side as you have runat=server in your first line?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
That just makes it such that there's a reference to it in the code-behind. It'll still run clientside code.
 
But you only call this function on the server validate e.g.
Code:
OnServerValidate="validateme"
Surely if you want client side validation you have have to use a javascript function on the ClientValidationFunction?




----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
ca8msm - yes it will still run on the server, but I've always found that the one way seems to work and the other doesn't :) All validator controls validate serverside, even the premade ones. The clientside javascript just makes it seamless (straight from MSDN).

you could also write a javascript function like the following:
Code:
<script language="JavaScript">
<!--
  function ValidateMe(sender, args)
  {
    args.IsValid = false;
  }
// -->
</script>
and insert it into the html of the page, then call this. This would make it run a little nicer on uplevel browsers.
 
Cheers guys, I actually worked it had to be Javascript just before i read the posts!!, Stars for the help though
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top