public knowledge blog_load(sender Virendra)

                                                                                                             Nothing shocks me, I am a Software Engineer.

Textbox with autopostback and regularexpression validator

Posted by Virendra Dugar on June 5, 2009

Suppose you have a textbox on your aspx page that has a autopostback set to true and It makes a call to server side function on TextChanged Event. You have also placed a RegularExpression Validator which validates the textbox value against the validation expression.

For example: You want to allow only numeric characters in the text box and you have placed a regular expression validator to achieve this. And you also want to do some server side operation on text change.

 TextBox and RegularExpression Validator
When you run the application and if you type any alphabets in the textbox, the regular expression validates the input, it shows the error message but the weird thing is it also does the post back and call the server side text change event and error message is gone. This is what you don’t want. TextChange event must be called only when inputs are correct. Then how do you do this?

A very simple solution. Just set the CausesValidation="true" for the textbox and you are done.

Enjoy…

2 Responses to “Textbox with autopostback and regularexpression validator”

  1. Thanks! Just what I was looking for.

    I only wish I’d found this before spending half a day trying to re-trigger the validation from javascript…

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>