Return false in form button click event to stop submission in JavaScript

Description

The following code shows how to return false in form button click event to stop submission.

Example


<html>
<head>
<script language="JavaScript">
<!-- w  w  w.j  a v  a2  s. c o  m-->
function chkForm()
{
if (form1.inp1.value == "")
{
alert("Please fill in the text box");
form1.inp1.focus();
return false;
}
}
</script>
</head>
<body>
<form name="form1" method="POST" action="">
<P><input type="text" name="inp1">
<input type="submit" value="Submit" onClick="return chkForm()">
<input type="reset" value="Reset"></p>
</form>
</body>
</html>

Click to view the demo

The code above generates the following result.

Return false in form button click event to stop submission in JavaScript
Home »
  Javascript Tutorial »
    Form »
      Button
Javascript Tutorial Button
Access form button with its name through do...
Access form with form name through document...
Add click action to a Button in JavaScript
Add click event handler for input button in...
Add click event handler to input onClick ev...
Change input button color and background co...
Get current value of the submit button whic...
List properties from a form button in JavaS...
Return false in form button click event to ...
Use Image as the submit button in JavaScrip...