Tuesday, January 19, 2010

Access Validation Controls with javascript

Asp.net Validation Controls:

Asp.net has many validation controls availables as native controls.this is the great way of validating INPUT controls at both   client side and server side.

some peoples hate this validation controls because they think that they are less managable from client side.but  this is not true.

when you  drop validation controls on the page and see SOURCE of that page. you can see that all validation stuff is shipped  as normal javascript variables. thus we can manage them from javascript easily.

i will show some useful methods/function in JAVASCRIPT  which may be  handy for us while using  validation controls .


(1) How to disable all validators:
  function DisaableAllValidators(sender)
  {

            var AllValidattors = new Array();
            AllValidators = Page_Validators;
            for (var i = 0; i < AllValidators.length; i++) {
                ValidatorEnable(AllValidators[i], false);
            }
 }
Here  Page_Validators is Asp.net generated array of validators.we can manipulate as we want in out javascript functions.
(2) Change validation messages:
 function ChangeValidationMessages()
 {

                var AllValidattors = new Array();
                AllValidators = Page_Validators;
                for (var i = 0; i < AllValidators.length; i++) {
                    AllValidators[i].errormessage = AllValidators[i].errormessage + "New";
                    AllValidators[i].innerHTML = AllValidators[i].innerHTML + "New";
                }           
 }
(3) Clear all validation Summaries:
 function ClearValidationSummaries()
 {

            var allSummaries = new Array();
            allSummaries = Page_ValidationSummaries;
            for (var i = 0; i < allSummaries.length; i++) {
                allSummaries[i].innerHTML = "";
            }
}
Here Page_ValidationSummaries is the Array of all validation summaries dropped on the page.you can manupulate all summaries using this array.
(4) Hide All validation messages:
   function HideAllValidators()
   {

            var AllValidattors = new Array();
            AllValidators = Page_Validators;
            for (var i = 0; i < AllValidators.length; i++) {
                if (AllValidators[i].display == 'Dynamic') {
                    AllValidators[i].style.display = 'none';
                }
                else {
                    AllValidators[i].style.visibility = 'hidden';
                }
            }
 }
(5)Force All validations to fired:
 function ForceAllValidation() {
            alert(Page_ClientValidate());
        }
Here Page_ClientValidate()  is the built-in function which is responsible to validate all validation controls on the page.
it accepts validationGroup as argument to validate  fire validation for particular validation group only.
(6)Force Single Validation to fired!:
   function ForceSingleValidation()
   {

            var tmpvalRequireMobile = document.all ? document.all["valRequireMobile"] : document.getElementById("valRequireMobile");
            ValidatorValidate(tmpvalRequireMobile);
            alert(tmpvalRequireMobile.isvalid);
    }
Here valRequireMobile Is ClientID of validator control and ValidatorValidate() is Built-in function which is accept validation control as argument and validate whether is is valid or not.

Thus,we can use Clientside validation library of validation controls to manepulate validation controls in custom javascript functions.



Thanks.