tags:

views:

16

answers:

1

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  A: 

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" />
Mikael Svenson
Perfect! Exactly what I was hoping for. Was unaware of the return true triggering a postback in this regard. Thanks.
bill