4
votes

I have an asp:button with an onclick property that posts back to the server. I would like to do some validation on the contents of a textbox before I fire the postback. I want to do the validation in javascript using some regex.

I cannot use an asp:XXXXvalidator; I just can't due to what my web app does.

Is there a way to call a javascript function from an asp:button that then calls the postback?

I know I can use OnClientClick to call js from the asp:button, but once I call the JS how do I signal that I want to postback the button?

1
What does your web app do exactly to break the validators? Also the terrible misuse of the word "postback" by pretty much all ASP.NET people is starting to bother me more and more. - Matti Virkkunen
I create dynamic elements with javascript. So I'm using this textbox button combo multiple times on the page. When a user does something I create a new set. I can't recreate the asp.net validation stuff client side when the element is copied without weird propagation stuff. - bill
Why can't you use "custom validator" javascript function? - Paco

1 Answers

4
votes

Create a javascript function which returns true/false on your check. If you return false, then the page will not be submitted to the server.

Pseudo-code:

<script type="text/javascript">
   function check()
   {
      if( valid ) return true;
      return false;
   }
</script>

<asp:Button ID="btnAdd" runat="server" OnClientClick="return check()" OnClick="click" />