Sunday, October 4, 2009

Client side validation of ASPX validation controls

This is the nice post under validating the ASPX validations in client side like java script. The concept behind is,

  • Some times we need to validate the .NET controls in client side whether they have correct values or not. We can do them in client side logic by using some existing functions available in javascript which are handled by ASP.NET framework.
  • And another requirement is on the page some controls are .NET controls and some are HTML controls. And all .NET controls are using the server side validation controls and html controls are using client side validation. So, when click on submit button, you need to detect whether form is valid or not on client side. So, you need to write some custom logic to detect all HTML controls are valid or not and then you need to detect all ASP.NET controls are having valid values or not. How will you detect that? You should validate both of them in javascript. So, this logic will help you to find the form is valid or not. 
function Page_ClientValidate(validationGroup) {
   Page_InvalidControlToBeFocused = null;
   if (typeof(Page_Validators) == "undefined") {
      return true;
   }
   var i;
   for (i = 0; i < Page_Validators.length; i++) {
      ValidatorValidate(Page_Validators[i], validationGroup, null);
   }
   ValidatorUpdateIsValid();
   ValidationSummaryOnSubmit(validationGroup);
   Page_BlockSubmit = !Page_IsValid;
   return Page_IsValid;
}

The above function is taking an argument named validationGroup. This is the validation group of the server side validation control. And the output or return value returning is the boolean value. If it is returning true then it passes the server side validation and false then it fails the server side validation. So, along with it's value you can write your own logic to validate the html controls and test whether form is valid or not.

Hope this helps and you can solve so many problems with this logic. Always welcome your valuable feedback and comments. Do you know any other ways to implement it?

2 comments: