Confirm pop-up before HTML form submission

I was messing around with some Javascript today, for use on a HTML form I was building. Here’s a simple code to present the user with a pop-up dialog box when they click a HTML form’s submit button, asking them “are you sure?”

<script type="text/javascript">
function confirmSubmit(){
  var r=confirm("are you sure?");
    if(r)
	  return true;
    return false;
    }
</script>

<form action="" onsubmit="return confirmSubmit()">
</form>

The Javascript confirm() function returns a boolean value, so test that value before proceeding to return true (proceed with form submittion) or false (halt the submission).

Update: I just got back from ice hockey practice, and over at practice, my hockey team member Scott asked what the purpose is of this blog post. The answer is, because this was what I did at work today. I was searching for a way to prompt a user after he/she hits the submit button on a HTML form. I don’t have a lot of code samples on my blog, although when I first started, I had thought of writing lots of technical howto’s with code samples. So I’ll try to write more of these types of blog posts. It’s mostly a matter of time, really. And oh, only the things that I _can_ reveal, such that it wouldn’t compromise my company, the Websense Security Labs. Or in any way infer what we are doing. We like to keep our labs a black box. Keep wondering :p