Saturday, November 8, 2008

Form validation before onClientClick

If I've understood correctly, you want to validate the page client-side before the user is asked to confirm that they want to submit the form? If that's right then I think you can use the Page_ClientValidate() function as suggested by siva_sm and the OnClientClick of your button, something like:
function ValidateMe()
{
var ok = Page_ClientValidate();
if(ok)
return confirm("Are you sure?");
else
return false;
}

I have just stumbled across a small problem. After the form validation is done, my drop down boxes will not perform a postback.

Any ideas??


I'm not sure if there is a better way to fix the problem, but I managed to replicate it and fix it. If you update the js function to this it should work:

function ValidateMe()
{
var ok = Page_ClientValidate();
if(ok)
{
return confirm("Are you sure?");
}
else
{
Page_BlockSubmit = false;
return false;
}
}
The Page_BlockSubmit variable is set to true when validation fails and stops the page being posted back on a subsequent try, even from a dropdown which isn't causing validation. This just happens the first time - change the value in the dropdown again and the page will postback (Page_BlockSubmit is reset to false the first time you select something in your dropdown so subsequent postbacks are fine). I hope that helps.

No comments: