Content: As we all know ASP.NET provide some validation extenders which can be implemented on controls to validate them at Client & Server side. Some of those validations extenders are :
. Required Field Validator (Validate the control for not filling up any information). . Range Validator (Validate the cotrol with respect to lower & upper bound). . Regular Expression Validator (Validate the control with respect to some regular expression).
. Custom Validator (Validate the control with some custom javascript code). etc…
To implement all these validation you don’t have to interact with Javascript(except custom validator where you have to write your own validation logic) as asp.net compiler create the associated Javascript code for validating the control at client side. All you have to do is to associate the validators with the respective controls & validation group may be. All these validation will occur only at the time of submitting the form and normally we don’t have any control over them. But if we want some control over the validation so that it will be compatible with our Javascript code these following functions may help you. Example : Suppose we want that at the time we are checked the publish checkbox of some article then it will validate some of the control & some complex logic like these.
Then we can call following Javascript functions on the click event of control for which we want to validate.
function manageValidation() {
Validate the Page:
// validate all the validation that are present in the page. Page_ClientValidate();
Validate the Page having certain validation Group:
// validate all the validation that are present in the page with this validation group. Page_ClientValidate('ValidationGroup');
Get Is the page valid:
// Check wheather the page is valid or not var pageValidated = Page_IsValid; if (pageValidated) { // Write some custom messages like alert('Thanks for signing up with us. we will send your password to respective mail account'); } else { // Other messages.. }
Enable or disable any validation :
// disable a certion validation // ValidatorEnable(Control Name, status) ValidatorEnable('requiredvalidatorTxtLastName', false) // Enable some validation. ValidatorEnable('requiredvalidatorTxtUserName', true)
Force Validation:
// Attach a certion validation with the control ValidatorHookupControl('txtUserName', 'requiredvalidatorTxtUserName') // Validate a certain validation. ValidatorValidate('requiredvalidatorTxtUserName') }
Manual Validation :
If you want to make the validation false of the page for any customize javascript Code you can do it as follows.
function checkPoints(sender,args) { if(document.getElementById('txtPoints').value == (parseInt( document.getElementById('txtBasicPoint').value) * 2) { args.isValid = false; } }
And attach this function with any control.
So when this piece of code will get executed it will set the isValid property of the page false. (Which should be true for making a post back) and hence we can ask the system to stop the postback.