Here is another trick to add custom validations or script while submiting a listitem in NewForm.aspx or EditForm.aspx.
When you click on OK or Submit in NewForm.aspx or EditForm.aspx, it will execute following script to submit the data into SharePoint DB.
if (!PreSaveItem()) return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(<>, '', true, '', '', false, true))
theForm.submit();
So, if you want to add custom validation or script before submiting, then add a Content Editor WebPart in the same page with following script.
< language="javascript">
var submitButton = document.getElementById("<>")
submitButton.onclick = updateButton;
function updateButton()
{
//
// Add custom code to validate a page or any other functionality
// which needs to be performed before submiting a page
//
if (!PreSaveItem()) return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(<>, '', true, '', '', false, true))
theForm.submit();
}
< / script>
in the sameway, you can add confirmation message for cancel button
function updateCancelButton()
{
var conf = confirm("Are you sure you want to cancel?");
if (conf)
{
STSNavigate('\u002f');return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(<>, '', true, '', '', false, true))
}
}
I hope this helps. Happy coding...