Client Side Validation using ASP.NET Validator Controls from Javascript

We use the server side control in ASP.NET and for validating them, we use the server validation control. These ASP.NET validation controls provide functionality to perform validation using client script but these are handled at the end of the page by rendered validation script. When we want to post the data by AJAX or JavaScript that time Validation control won’t fire the validation because we are preventing the script to move ahead by returning the page there itself. If page process till the end of the page then script will execute and validation would be fired.

By default, when client-side validation is being performed, the user cannot post the page to the server if there are errors on the page thus the user experience with the page is enhanced.
There are some Client-side objects and Client-side APIs which help us to use client side validation using ASP.NET server controls from JavaScript :

1. Page_IsValid:
It is a boolean Variable which represents whether the page is currently valid or not.

2. Page_Validators
It’s an Array of elements which represents an array containing all of the validators on the page.

3. isvalid
It is a Boolean property of each client validator indicates the validator’s validity.

4. Page_ValidationActive
It is a boolean variable which is used for enabling or disabling the validation on the page programmatically.

5. Page_ValidationSummaries
It is an array containing of all of the validation summaries on the page.

Methods
1. ValidatorValidate(validator)
It takes client-validator as its input parameter and check this validator and update its display.

2. ValidatorEnable(validator, enable)
Takes 2 parameters: client validator and boolean value to enable or disable
It is used to enable or disable a client validator. On disable, validator will not be evaluating the validator.

3. ValidatorHookupControl(control, validator)
Takes 2 parameters: HTML element as control and client validator
It is used to modifies or creates the element’s change event so that it updates the validator when changed. This can be useful for custom validators that depend on multiple input values.

4. Page_ClientValidate(validatorGrp)
Takes 1 parameter : Validation group name
Return : bool value
It is used to validate all validator of that given validation group and returns bool value.

Example :

Check Validation group

In alternate way, we can use like this

Enable/Disable Validator using javascript:

Other properties and methods

You Might Interested In