Manual validation with asp.net validation controls
You all know that asp.net validation controls are automatically validated when you set property CausesValidation=true for a button. But sometimes we will be required to do manually/explicitly. I came across with this situation. That is, on button click I need to execute some JavaScript code if all validations are passed. The problem here is both validations and JavaScript code will not get executed same time. In this case what I did is, I set CausesValidation=false for a button and added the following JavaScript code to do manual validation.
If it is passed then I am executing required JavaScript code else not.
function checkValidations() {
var
rtnVal = true;
for
(i = 0; i < Page_Validators.length; i++) {
ValidatorValidate(Page_Validators[i]);
if (!Page_Validators[i].isvalid) { //at least one is not valid.
rtnVal = false;
break; //exit
for-loop, we are done.
}
}
return
rtnVal;
}
If it is passed then I am executing required JavaScript code else not.
<asp:Button ID="btnSubmit"
runat="server"
OnClientClick="return
doSomeThing()" OnClick="btnSubmit_Click" Text="Submit">
</asp:Button>
function doSomeThing()
{
if(checkValidations())
{
//perform
something
return
true;
}
else
return
false;
}
Comments
Post a Comment