I’ve finally gotten around to blogging about this. I’ve run across several people asking similar questions and decided it may make a decent blog post.
A common issue when making use of client-side code and Ajax is the validation of controls on the client. For example, the submit button is clicked, however the page isn’t posted to the server in a normal fashion, rather, a custom client-side event handler has been defined so that we can perform Ajax operations, that is, send results to the server via client-side proxy web services. So what’s the problem you may be asking yourself? What if we want to make use of the asp:RequiredFieldValidator control to validate multiple textbox controls? Well, we could manually check the content of those textbox controls and if any are empty notify the user and terminate execution of the method; however that defeats the purpose of the RequiredFieldValidator control.
A better approach is to invoke the Framework supplied client-side method Page_ClientValidate(). This method accepts a single argument which is that of the validation group and is optional. For example, consider the following JavaScript code:
function processOk()
{
var validated = Page_ClientValidate('VG');
if(!validated)
return false;
// the remaining portion of or code should follow
}
If the controls in the specified validation group validates successfully you can invoke your web service passing the desired data, however in the above example if the validation fails we simply terminate execution of the method, the RequiredFieldValidator controls will then display their corresponding error messages (such as the red asterisk alongside the textbox whose content is missing, etc). You may wish to notify the user of the errors by way of a popup or alert() message as well.
As always, happy coding! Feel free to drop me an email!