I was using jQuery datepicker for one on my test project. The asp.net textbox it was linked to was having RequiredFieldValidator(RFV) and a RegularExpressionValidaton(REV).
If I simply press my button without entering anything in the textbox, the RFV would fire and show me the error message. Now when I selected a date from the jquery datepicker the error message still stayed there which I wanted it to disappear. The same thing happened for REV as well i.e.after selecting a date using datepicker, if I manually change it to some invalid date, the REV would fire-up and show me the regex error message. But upon re-selecting a valid date from the datepicker, the message won't go.
After searching the web, here is what I came up with.
The first block/ function is where I hookup my textbox with the jQuery datepicker. Note the syntax ($("[id$='_txtDate']")) I am using to find my asp.net TextBox. Initialize the datepicker and set the handler for onSelect event. This is the event that is fired when you select a date in the datepicker.
The onSelectHandler is the function that is called when onSelect event is fired. Here we see if we have a client-side validation function and if there is one then call that function pass the id of our textbox. We get that via $(this).attr('id'). this refers to our textbox instance. That's it, if the value is valid the error message will disappear.
This was my first attempt assuming I have got it right but no it had a flaw. The Page_ClientValidate function takes a 'validationgroup' as a parameter. So from my standpoint I was passing a "control id" but the function won't. Since it could not find any match for the value I was passing, it wasn't actually validating but just ignoring the validation and passing back true i.e. everything is valid. This causes every validation message to be made hidden.
There was a partial answer to my requirement and below is the modified onSelectHandler event:
The key points from the second link were: (a) Asp.net Validators are rendered as 'span' tags and (b) You can access the controltovalidate property of the validator using the instance of that span. The (b) was the click that I can then get the validationGroup the same way, which I can then pass to Page_ClientValidate function. So that is the code.
Basically we are just forcing validation on our textbox upon date selection in the datepicker. Simply calling Page_ClientValidate() was not my choice because my textbox was inside a databound control. I spent time to see if I can get hold of ValidationGroup but finally ended up with this which is more relevant in my case.
Sorry this is a quick post. I might come back and modify it but for now just want to put the code up there. Thanks and let me know if you have any questions or any other comments.
Update-1: I found this solution has an issue. Upon date select it does clear out 'All' the validation error messages for other controls as well and not just for the single input textbox. I will post back if I find an alternative.
Update-2: Added another code snippet that seems to be working.