5

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?

3
  • 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. Commented Sep 18, 2010 at 18:55
  • 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. Commented Sep 18, 2010 at 18:58
  • Why can't you use "custom validator" javascript function? Commented Sep 18, 2010 at 19:55

1 Answer 1

5

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" />
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Exactly what I was hoping for. Was unaware of the return true triggering a postback in this regard. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.