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.
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.