Sunday, September 4, 2011

Validation for a particular button click event in javascript

In asp.net form assume we have validations for some of the controls, For suppose if we navigate/move from one control to other these validations will fire most of the times. These validations fire for OnChange, OnBlur and other events also. we dont require these validations always. when we submit the form then only we need validations or when we click 'ok/submit' button these should get fire.
Here is the solution for it.
asp.net
  

OnChange="javascript:DropDownList1_OnChange()">

list item1
list item2

ControlToValidate="DropDownList1"
SetFocusOnError="true"
ErrorMessage="Select a value for 'DropDownList1'"
Display="None" />

javascript
  



when you navigate to other controls/select items in the drop down list validator fires always.
if you want to stop this behaviour.
  

function pageLoad() {
DisableAllValidations(false);
}
function DisableAllValidations(enabled1) {
for (k = 0; k < Page_Validators.length; k++) {
Page_Validators[i].enabled = enabled1;
Page_Validators[i].isvalid = enabled1;
}
}

it stops all the validations in the pageLoad() itself.
But when you want to enable validations for a particular button click, write below code.
  

$("input[value='ok']").click(function () {
DisableAllValidations(true);
Page_ClientValidate();
if (!Page_IsValid) {
DisableValidators(false);
}
return Page_IsValid;
}

No comments:

Post a Comment