I have an asp:textbox. On textchange of this textbox, I'm doing validation for the text entered. If the text entered is incorrect, I want to flash a message of incorrect text entered. Please re-enter. How can I do this in ASP?
+1
A:
Just use a RegularExpressionValidator and keep client validation property at it's default "true" value. The control will handle this behavior for you.
Here's an example in action with the code: http://www.w3schools.com/ASPNET/showasp.asp?filename=demo_regularexpvalidator
David Stratton
2010-08-19 06:56:27
Clarification - this assumes you want to do it client-side without a postback, as @Joel Coehoorn pointed out, it would be a bad user experience to have to do a postback.
David Stratton
2010-08-19 07:09:12
I've to do a postback.The Validation I'm doing is stored in a datarow array.Hence I've to compare the text value in that array and then respond if the text entered is correct or not.
gizgok
2010-08-19 17:35:11
Well, this will work with a Postback if you set the client validtion property to false. Might I recommend using an Ajax UpdatePanel to hide the postbacks?
David Stratton
2010-08-19 18:27:58
A:
You should avoid using that property if you can. Your validation code will only run when you postback to the server, and you don't want to do a full postback every time the text changes unless you really have to. Instead, use javascript to handle the onchange event of the input element rendered in the raw html by asp.net. Then remember to duplicate your validation code on the server.
Joel Coehoorn
2010-08-19 06:57:46