How to alert User when the form is Submitted or Posted to Server using jQuery in ASP.Net?
Sometimes, we require intercepting whenever a page is posted or submitted to the server.
For example, in a input form where it allows user to save the data only once it will be better if we can ask the user some sort of confirmation before submitting the page. This feature will give an opportunity to the users to review their input again. Another example will be, asking the user confirmation before doing any unique actions which cannot be reverted back in our applications. Since, jQuery has become most widely used client scripting library i will demonstarte this requirement using jQuery library.
The below jQuery snippet will alert the user with a confirm box with teh message "Are you sure to continue?" Clicking "Yes" will post the page and "No" will cancel the submit.
< head runat="server">
<title></title>
<script src="_scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script language="javascript">
$(document).ready( function() {
$( "form").submit(function() {
return confirm('Are you sure to continue?');
});
});
</ script>
Happy Coding!!
|