- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to prevent form from being submitted?
We can easily prevent forms on a web page to submit using the event.preventDefault() or returning Flase on submit event handler. Let us see the example one by one −
Prevent form from being submitted using the event.preventDefault()
To prevent form from being submitted, use the event.preventDefault() i.e. −
<form onsubmit="event.preventDefault();">
Example
Let us see the complete example −
<!DOCTYPE html> <html> <body> <h1>Details</h1> <form onsubmit="event.preventDefault();"> <p>Select the subject you want to choose:</p> <div> <input type="radio" id="prog" name="subject" value="prog"> <label for="prog">Programming</label> <input type="radio" id="dev" name="subject" value="dev"> <label for="dev">Web Development</label> <input type="radio" id="db" name="subject" value="db"> <label for="db">Database</label> </div><br/> <div> <button id="submit" type="submit">Submit</button> </div> </form> </body> </html>
Output

Select any option and click Submit. The form won’t submit −

Prevent form from being submitted using the return false on submit event handler
To prevent form from being submitted, use the return false on submit event handler −
<form onsubmit="return false;">
Example
Let us see the complete example −
<!DOCTYPE html> <html> <body> <h1>Details</h1> <form onsubmit="return false;"> <p>Select the subject you want to choose:</p> <div> <input type="radio" id="prog" name="subject" value="prog"> <label for="prog">Programming</label> <input type="radio" id="dev" name="subject" value="dev"> <label for="dev">Web Development</label> <input type="radio" id="db" name="subject" value="db"> <label for="db">Database</label> </div><br/> <div> <button id="submit" type="submit">Submit</button> </div> </form> </body> </html>
Output

Select any option and click Submit. The form won’t submit −

- Related Articles
- How to Prevent Twitter Accounts from being Hacked
- How to prevent numbers being changed to exponential form in Python Matplotlib?
- How to prevent raw rice from being destroyed by termites?
- How can I prevent a window from being resized with Tkinter?
- WebGL: Prevent color buffer from being cleared in HTML5
- How to specify where to send the form-data when a form is submitted in HTML?
- How to prevent a background process from being stopped after closing SSH client in Linux?
- Set where to send the form-data when a form is submitted in HTML?
- Prevent a combination of items from being inserted twice in MySQL?
- How to Prevent Systems from Phishing Attacks?
- How to Prevent Your Website from Hackers?
- How to prevent multiple inserts when submitting a form in PHP?
- How to prevent the screen from sleeping in iOS?
- How to prevent moment.js from loading locales with webpack?
- How to prevent buttons from submitting forms in HTML?

Advertisements